[PATCH 3/3] tools/mm: grow the record list instead of guessing its size
From: Henrique Godoy
Date: Wed Nov 04 2026 - 14:38:29 EST
main() sizes the array of records with st.st_size / 100, and add_list()
refuses to go past it with "max_size too small??". A dump whose records
are shorter than 100 bytes holds more records than that, so a valid input
is rejected and no output is written:
$ ./page_owner_sort dump.txt out.txt
max_size too small??
Keep the guess as a starting point and grow the array with realloc().
Co-developed-by: Pierre <pierre@xxxxxxxxxxxx>
Signed-off-by: Pierre <pierre@xxxxxxxxxxxx>
Signed-off-by: Henrique Godoy <henrique.godoy@xxxxxxxxxxxx>
---
tools/mm/page_owner_sort.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/tools/mm/page_owner_sort.c b/tools/mm/page_owner_sort.c
index a042bf1..5b51cff 100644
--- a/tools/mm/page_owner_sort.c
+++ b/tools/mm/page_owner_sort.c
@@ -571,8 +571,16 @@ static bool add_list(char *buf, int len, char *ext_buf)
enum FILTER_RESULT filter_result;
if (list_size == max_size) {
- fprintf(stderr, "max_size too small??\n");
- return false;
+ struct block_list *grown;
+ int new_size = max_size ? max_size * 2 : 64;
+
+ grown = realloc(list, new_size * sizeof(*list));
+ if (!grown) {
+ fprintf(stderr, "Out of memory\n");
+ return false;
+ }
+ list = grown;
+ max_size = new_size;
}
filter_result = filter_record(buf);
if (filter_result == FILTER_ERROR) {
@@ -952,7 +960,8 @@ int main(int argc, char **argv)
goto out_module;
fstat(fileno(fin), &st);
- max_size = st.st_size / 100; /* hack ... */
+ /* the guess is only a starting point, add_list() grows the list */
+ max_size = st.st_size / 100 ?: 1;
list = malloc(max_size * sizeof(*list));
buf = malloc(BUF_SIZE);
--
2.39.5