From 5fc450a78a031722f9ed7fcb869d28b7c603dc98 Mon Sep 17 00:00:00 2001 From: mephistolist <49227141+mephistolist@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:48:03 +0000 Subject: [PATCH 1/2] Update mem.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed this warning: src/mem.c: In function ‘dbg_realloc’: src/mem.c:107:5: warning: pointer ‘ptr’ used after ‘realloc’ [-Wuse-after-free] 107 | unlog_ptr(ptr, file, line); | ^~~~~~~~~~~~~~~~~~~~~~~~~~ src/mem.c:103:20: note: call to ‘realloc’ here 103 | void* newptr = realloc(ptr, size); | ^~~~~~~~~~~~~~~~~~ --- src/mem.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/mem.c b/src/mem.c index d62ba60..8feda23 100644 --- a/src/mem.c +++ b/src/mem.c @@ -1,4 +1,3 @@ - #include #include #include @@ -8,8 +7,6 @@ #include "../cutils.h" - - #define MAX_PTRS 8192 typedef struct { @@ -97,15 +94,24 @@ void* dbg_calloc(size_t nmemb, size_t size, char* file, int line) return ptr; } -void* dbg_realloc(void* ptr, size_t size, char* file, int line) -{ +void* dbg_realloc(void* ptr, size_t size, char* file, int line) { dbg(4, "dbg_realloc: %s:%d %p %zu->%zu bytes", file, line, ptr, malloc_usable_size(ptr), size); + + // Unlog the pointer before reallocating + unlog_ptr(ptr, file, line); + + // Reallocate memory void* newptr = realloc(ptr, size); + + // Check for allocation failure if (newptr == NULL) { msg(ERROR, "\trealloc failed"); + return NULL; // Return NULL on failure } - unlog_ptr(ptr, file, line); + + // Log the new pointer log_ptr(newptr, file, line); + return newptr; } @@ -136,4 +142,3 @@ void dbg_free(void* ptr, char* file, int line) } free(ptr); } - From 81d4d1a0665fb2f5b803b5fc044b270f1c623f78 Mon Sep 17 00:00:00 2001 From: mephistolist <49227141+mephistolist@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:53:37 +0000 Subject: [PATCH 2/2] Update system.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added some code to resolve this warning: src/system.c: In function ‘mvlink’: src/system.c:212:46: warning: ‘strncat’ specified bound 1 equals source length [-Wstringop-overflow=] 212 | if (new_link[strlen(new_link)-1] != '/') strncat(new_link,"/",1); | ^~~~~~~~~~~~~~~~~~~~~~~ --- src/system.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/system.c b/src/system.c index 7b9b4f1..575a984 100644 --- a/src/system.c +++ b/src/system.c @@ -209,7 +209,9 @@ int mvlink(char* old_path,char* new_path) //strncpy(new_link,parent_path,strlen(parent_path)); // add the separator - if (new_link[strlen(new_link)-1] != '/') strncat(new_link,"/",1); + if (new_link[strlen(new_link) - 1] != '/') { + strncat(new_link, "/", 2); // Reserve space for the null terminator + } //strncat(new_link,rel_path,strlen(rel_path)); @@ -289,4 +291,3 @@ int mvsp(char* old_path,char* new_path) return 0; } -