]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
sort: fix bug on 64-bit hosts with at least 32768 processors
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 2 Dec 2010 05:50:00 +0000 (21:50 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 2 Dec 2010 05:51:15 +0000 (21:51 -0800)
* src/sort.c (MAX_MERGE): Avoid integer overflow when on a machine
with (say) 32-bit int and 64-bit size_t and when level == 15.
Without this fix, on such a machine with 32768 or more processors,
the level computation could overflow on large input, and this
would result in division by zero.

src/sort.c

index 1aa1eb4163bdb7e0da483f4d8bad91654938e177..5c368cd955e08dc20073c8b9727abee931d068f1 100644 (file)
@@ -107,7 +107,7 @@ struct rlimit { size_t rlim_cur; };
 /* Maximum number of lines to merge every time a NODE is taken from
    the MERGE_QUEUE.  Node is at LEVEL in the binary merge tree,
    and is responsible for merging TOTAL lines. */
-#define MAX_MERGE(total, level) ((total) / ((2 << level) * (2 << level)) + 1)
+#define MAX_MERGE(total, level) (((total) >> (2 * ((level) + 1))) + 1)
 
 /* Heuristic value for the number of lines for which it is worth
    creating a subthread, during an internal merge sort, on a machine