Skip to contentSkip to conversation

tools/vm/page_owner_sort.c: Fix NULL-pointer dereference when comparing stack traces

PATCH New Sean Anderson 1 participant 1 message Nov 25, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/vm/page_owner_sort.c: Fix NULL-pointer dereference when comparing stack traces

If there is no newline in a block, then strchr returns NULL. We check for this in stacktrace_compare, but not when culling. Fix this (and any future bugs like it) by replacing NULL stack traces with "" in add_list.

Fixes d0abbab9e9e9 ("tools/vm/page_owner_sort.c: sort by stacktrace before culling")

---

 tools/vm/page_owner_sort.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/vm/page_owner_sort.c +2 -2
index b91d3381300c..1b2acf02d3cd 100644
@@ -55,7 +55,7 @@ static int compare_stacktrace(const void *p1, const void *p2)
 {
 	const struct block_list *l1 = p1, *l2 = p2;
 
-	return strcmp(l1->stacktrace ?: "", l2->stacktrace ?: "");
+	return strcmp(l1->stacktrace, l2->stacktrace);
 }
 
 static int compare_num(const void *p1, const void *p2)
@@ -121,7 +121,7 @@ static void add_list(char *buf, int len)
 	list[list_size].page_num = get_page_num(buf);
 	memcpy(list[list_size].txt, buf, len);
 	list[list_size].txt[len] = 0;
-	list[list_size].stacktrace = strchr(list[list_size].txt, '\n');
+	list[list_size].stacktrace = strchr(list[list_size].txt, '\n') ?: "";
 	list_size++;
 	if (list_size % 1000 == 0) {
 		printf("loaded %d\r", list_size);
Signature · 1 line
2.33.0