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

From: Sean Anderson
Date: Thu Nov 25 2021 - 11:47:12 EST


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")
Signed-off-by: Sean Anderson <seanga2@xxxxxxxxx>
---

tools/vm/page_owner_sort.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/vm/page_owner_sort.c b/tools/vm/page_owner_sort.c
index b91d3381300c..1b2acf02d3cd 100644
--- a/tools/vm/page_owner_sort.c
+++ b/tools/vm/page_owner_sort.c
@@ -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);
--
2.33.0