[PATCH 1/3] tools/mm: keep the last record of a dump

From: Henrique Godoy

Date: Wed Nov 04 2026 - 15:02:44 EST


read_block() returns -1 to the caller when it reaches the end of the file,
and main() reads that as "no more records". A record followed by the end
of the file, instead of by an empty line, is read into the buffer and then
dropped: a dump of ten records without the final empty line loads nine,
and the tenth disappears without a word.

Return what was accumulated when the end of the file arrives in the middle
of a record.

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 | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/mm/page_owner_sort.c b/tools/mm/page_owner_sort.c
index 6c2ac5d..4c2a4e4 100644
--- a/tools/mm/page_owner_sort.c
+++ b/tools/mm/page_owner_sort.c
@@ -134,7 +134,13 @@ int read_block(char *buf, char *ext_buf, int
buf_size, FILE *fin)
curr += strlen(curr);
}

- return -1; /* EOF or no space left in buf. */
+ if (feof(fin)) {
+ if (curr > buf)
+ return curr - buf; /* record without the trailing empty line */
+ return -1;
+ }
+
+ return -1; /* no space left in buf. */
}

static int compare_txt(const void *p1, const void *p2)
--
2.39.5