diff --git a/ChangeLog b/ChangeLog index 23a0331a9f1c7e9e44dbb324785c2a8442dc785f..1d318208af2aade2bb621bd71e4a4f693ff92f59 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2018-07-13 Niels Möller <nisse@lysator.liu.se> + + * examples/eratosthenes.c (vector_alloc): Add assert related to + overflow in the size calculation. Fixes a corner case identified + by static analysis. + 2018-07-12 Niels Möller <nisse@lysator.liu.se> * examples/eratosthenes.c (main): Don't allocate bitmap storage diff --git a/examples/eratosthenes.c b/examples/eratosthenes.c index 711618200e86b6aa999025b4eb1fbd9c0c9a9ecc..35f84e1c00a916611fbe8777209e005e82d011fe 100644 --- a/examples/eratosthenes.c +++ b/examples/eratosthenes.c @@ -92,8 +92,13 @@ isqrt(unsigned long n) static unsigned long * vector_alloc(unsigned long size) { - unsigned long end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG; - unsigned long *vector = malloc (end * sizeof(*vector)); + unsigned long end; + unsigned long *vector; + + assert (size <= ULONG_MAX - (BITS_PER_LONG - 1)); + + end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG; + vector = malloc (end * sizeof(*vector)); if (!vector) {