Skip to contentSkip to conversation

perf tests: use strdup() in "Object code reading"

PATCH Superseded James Clark 2 participants 2 messages Oct 7, 2025 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

James Clark

author reviewer
#0 lore raw

PATCH 1/1 perf tests: use strdup() in "Object code reading"

Use strdup() instead of fixed PATH_MAX buffer for storing paths to not waste memory.

---
Applies on top of: https://lore.kernel.org/linux-perf-users/CAP-5=fV1N-j+f4GBFnDWsmoMZcz_k0U=nu1A7NZz-g4gzCH4KA@mail.gmail.com/T/#t

 tools/perf/tests/code-reading.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/tests/code-reading.c +3 -2
index 4c9fbf6965c4..88408eea9e41 100644
@@ -43,7 +43,7 @@
 struct tested_section {
 	struct rb_node rb_node;
 	u64 addr;
-	char path[PATH_MAX];
+	char *path;
 };
 
 static bool tested_code_insert_or_exists(const char *path, u64 addr,
@@ -79,7 +79,7 @@ static bool tested_code_insert_or_exists(const char *path, u64 addr,
 		return true;
 
 	data->addr = addr;
-	strlcpy(data->path, path, sizeof(data->path));
+	data->path = strdup(path);
 	rb_link_node(&data->rb_node, parent, node);
 	rb_insert_color(&data->rb_node, tested_sections);
 	return false;
@@ -94,6 +94,7 @@ static void tested_sections__free(struct rb_root *root)
 						     rb_node);
 
 		rb_erase(node, root);
+		free(ts->path);
 		free(ts);
 	}
 }
Signature · 1 line
2.34.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 26 lines · James Clark

Use strdup() instead of fixed PATH_MAX buffer for storing paths to not waste memory.

---
Applies on top of: https://lore.kernel.org/linux-perf-users/CAP-5=fV1N-j+f4GBFnDWsmoMZcz_k0U=nu1A7NZz-g4gzCH4KA@mail.gmail.com/T/#t

 tools/perf/tests/code-reading.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/tests/code-reading.c +1 -1
index 4c9fbf6965c4..88408eea9e41 100644
@@ -43,7 +43,7 @@
 struct tested_section {
        struct rb_node rb_node;
        u64 addr;
-       char path[PATH_MAX];
+       char *path;
 };
 
 static bool tested_code_insert_or_exists(const char *path, u64 addr,

tools/perf/tests/code-reading.c static bool tested_code_insert_or_exists(const char *path, u64 addr, · line 82

Quoted 5 lines
tools/perf/tests/code-reading.c +1 -1
@@ -79,7 +79,7 @@ static bool tested_code_insert_or_exists(const char *path, u64 addr,
                return true;
 
        data->addr = addr;
-       strlcpy(data->path, path, sizeof(data->path));
+       data->path = strdup(path);

nit: should really check the memory allocation succeeded

```
if (!data->path) {
   free(data);
   return true;
}
```

Otherwise:

Thanks,
Ian

Quoted 14 lines
tools/perf/tests/code-reading.c +1 -1
        rb_link_node(&data->rb_node, parent, node);
        rb_insert_color(&data->rb_node, tested_sections);
        return false;
@@ -94,6 +94,7 @@ static void tested_sections__free(struct rb_root *root)
                                                     rb_node);
 
                rb_erase(node, root);
+               free(ts->path);
                free(ts);
        }
 }
--

2.34.1