aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Pipping <sebastian@pipping.org>2023-11-14 00:13:20 +0100
committerSebastian Pipping <sebastian@pipping.org>2023-11-14 00:59:53 +0100
commit09cc3d484d4c31578bc786e984bd90c9181acabb (patch)
tree1fe6930d27ff7d6d756f628e1934154cd5cb32a3
parent20d8d5eef009d29df457abe8fb40ea615971dedc (diff)
downloadbindfs-09cc3d484d4c31578bc786e984bd90c9181acabb.tar.gz
misc.c: Fix overflow detection zombie in function grow_memory_block
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 > | ^
-rw-r--r--src/misc.c9
1 files changed, 5 insertions, 4 deletions
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 <stdarg.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -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;