Commit dd279150 authored by Niels Möller's avatar Niels Möller

Check for allocation overflow in eratosthenes program.

parent 95798b5c
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
......
......@@ -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)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment