Skip to content
Snippets Groups Projects
Commit dd279150 authored by Niels Möller's avatar Niels Möller
Browse files

Check for allocation overflow in eratosthenes program.

parent 95798b5c
No related branches found
No related tags found
No related merge requests found
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> 2018-07-12 Niels Möller <nisse@lysator.liu.se>
* examples/eratosthenes.c (main): Don't allocate bitmap storage * examples/eratosthenes.c (main): Don't allocate bitmap storage
......
...@@ -92,8 +92,13 @@ isqrt(unsigned long n) ...@@ -92,8 +92,13 @@ isqrt(unsigned long n)
static unsigned long * static unsigned long *
vector_alloc(unsigned long size) vector_alloc(unsigned long size)
{ {
unsigned long end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG; unsigned long end;
unsigned long *vector = malloc (end * sizeof(*vector)); 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) if (!vector)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment