From 20d8d5eef009d29df457abe8fb40ea615971dedc Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Tue, 14 Nov 2023 00:09:34 +0100 Subject: misc.c: Address warning -Wsign-compare MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The symptom: > src/misc.c: In function ‘path_starts_with’: > src/misc.c:169:45: error: operand of ‘?:’ changes signedness from ‘long int’ to ‘long unsigned int’ due to unsignedness of other operand [-Werror=sign-compare] > 169 | size_t path_part_len = path_slash ? path_slash - path_part : path_len - (path_part - path); > | ^~~~~~~~~~~~~~~~~~~~~~ --- src/misc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/misc.c') diff --git a/src/misc.c b/src/misc.c index 122497d..b937075 100644 --- a/src/misc.c +++ b/src/misc.c @@ -166,7 +166,7 @@ bool path_starts_with(const char *path, const char* prefix, size_t prefix_len) const char* path_part = path + (prefix_part - prefix); const char* path_slash = strchr(path_part, '/'); - size_t path_part_len = path_slash ? path_slash - path_part : path_len - (path_part - path); + size_t path_part_len = path_slash ? (size_t)(path_slash - path_part) : path_len - (path_part - path); return prefix_part_len == path_part_len; } -- cgit v1.2.3 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/misc.c') 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