Commit 0b0c2314 authored by Niels Möller's avatar Niels Möller

Additional analogous assert in eratosthenese program.

parent dd279150
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
* examples/eratosthenes.c (vector_alloc): Add assert related to * examples/eratosthenes.c (vector_alloc): Add assert related to
overflow in the size calculation. Fixes a corner case identified overflow in the size calculation. Fixes a corner case identified
by static analysis. by static analysis.
(vector_init): Analogous assert.
2018-07-12 Niels Möller <nisse@lysator.liu.se> 2018-07-12 Niels Möller <nisse@lysator.liu.se>
......
...@@ -111,8 +111,10 @@ vector_alloc(unsigned long size) ...@@ -111,8 +111,10 @@ vector_alloc(unsigned long size)
static void static void
vector_init(unsigned long *vector, unsigned long size) vector_init(unsigned long *vector, unsigned long size)
{ {
unsigned long end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG; unsigned long end, i;
unsigned long i;
assert (size <= ULONG_MAX - (BITS_PER_LONG - 1));
end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG;
for (i = 0; i < end; i++) for (i = 0; i < end; i++)
vector[i] = ~0UL; vector[i] = ~0UL;
......
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