+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
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)
{