From 883f24cb32f6373291985ab9c079bcf44dbdcc58 Mon Sep 17 00:00:00 2001
From: Remi Rampin <remi@rampin.org>
Date: Mon, 25 Nov 2024 17:05:16 -0500
Subject: [PATCH 1/2] Grow the path buffer when reading /proc/.../maps

---
 native/tracer.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

--- a/native/tracer.c
+++ b/native/tracer.c
@@ -231,7 +231,9 @@
     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);
+    previous_path[0] = 0;
 
     const char *const fmt = "/proc/%d/maps";
     int len = snprintf(&dummy, 1, fmt, tid);
@@ -301,7 +303,7 @@
         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 +311,22 @@
                 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;
 }
 
