From 09cc3d484d4c31578bc786e984bd90c9181acabb Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Tue, 14 Nov 2023 00:13:20 +0100 Subject: misc.c: Fix overflow detection zombie in function grow_memory_block MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Started out with this warning: > src/misc.c: In function ‘grow_memory_block’: > src/misc.c:333:25: error: comparison of unsigned expression in ‘< 0’ is always false [-Werror=type-limits] > 333 | if (new_cap < 0) { // Overflow > | ^ --- src/misc.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/misc.c b/src/misc.c index b937075..fdebff9 100644 --- a/src/misc.c +++ b/src/misc.c @@ -19,6 +19,7 @@ #include "misc.h" #include +#include #include #include #include @@ -328,12 +329,12 @@ void grow_memory_block(struct memory_block *a, size_t amount) if (new_cap == 0) { new_cap = 8; } else { + if (new_cap > SIZE_MAX / 2) { + fprintf(stderr, "Memory block too large."); + abort(); + } new_cap *= 2; } - if (new_cap < 0) { // Overflow - fprintf(stderr, "Memory block too large."); - abort(); - } } a->ptr = (char *)realloc(a->ptr, new_cap); a->capacity = new_cap; -- cgit v1.2.3