From 883f24cb32f6373291985ab9c079bcf44dbdcc58 Mon Sep 17 00:00:00 2001 From: Remi Rampin Date: Mon, 25 Nov 2024 17:05:16 -0500 Subject: [PATCH] Grow the path buffer when reading /proc/.../maps --- reprozip/native/tracer.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/reprozip/native/tracer.c b/reprozip/native/tracer.c index 803b2042..326e9a28 100644 --- a/reprozip/native/tracer.c +++ b/reprozip/native/tracer.c @@ -231,7 +231,8 @@ int trace_add_files_from_proc(unsigned int process, pid_t tid, char dummy; char *line = NULL; size_t length = 0; - char previous_path[4096] = ""; + size_t previous_path_size = 4096; + char *previous_path = malloc(previous_path_size); const char *const fmt = "/proc/%d/maps"; int len = snprintf(&dummy, 1, fmt, tid); @@ -301,7 +302,7 @@ int trace_add_files_from_proc(unsigned int process, pid_t tid, if(inode > 0) { if(strcmp(pathname, binary) != 0 - && strncmp(pathname, previous_path, 4096) != 0) + && strcmp(pathname, previous_path) != 0) { #ifdef DEBUG_PROC_PARSER log_info(tid, " adding to database"); @@ -309,11 +310,22 @@ int trace_add_files_from_proc(unsigned int process, pid_t tid, if(db_add_file_open(process, pathname, FILE_READ, path_is_dir(pathname)) != 0) return -1; - strncpy(previous_path, pathname, 4096); + { + size_t needed_size = strlen(pathname) + 1; + if(needed_size > previous_path_size) { + while(needed_size > previous_path_size) { + previous_path_size *= 2; + } + free(previous_path); + previous_path = malloc(previous_path_size); + } + } + strcpy(previous_path, pathname); } } } fclose(fp); + free(previous_path); return 0; }