From 0b0c23141d82a04ff9cc4815524c6e4a55434039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se> Date: Fri, 13 Jul 2018 20:18:46 +0200 Subject: [PATCH] Additional analogous assert in eratosthenese program. --- ChangeLog | 1 + examples/eratosthenes.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1d318208..b6820bb7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,7 @@ * examples/eratosthenes.c (vector_alloc): Add assert related to overflow in the size calculation. Fixes a corner case identified by static analysis. + (vector_init): Analogous assert. 2018-07-12 Niels Möller <nisse@lysator.liu.se> diff --git a/examples/eratosthenes.c b/examples/eratosthenes.c index 35f84e1c..38574778 100644 --- a/examples/eratosthenes.c +++ b/examples/eratosthenes.c @@ -111,8 +111,10 @@ vector_alloc(unsigned long size) static void vector_init(unsigned long *vector, unsigned long size) { - unsigned long end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG; - unsigned long i; + unsigned long end, i; + + assert (size <= ULONG_MAX - (BITS_PER_LONG - 1)); + end = (size + BITS_PER_LONG - 1) / BITS_PER_LONG; for (i = 0; i < end; i++) vector[i] = ~0UL; -- GitLab