[PATCH 2/3] tools/mm: do not mistake a full buffer for the end of the file

From: Henrique Godoy

Date: Wed Nov 04 2026 - 15:07:08 EST


read_block() returns -1 both at the end of the file and when a single
record does not fit in BUF_SIZE. main() reads the second case as "no more
records", so a dump carrying a long record is sorted as if the file ended
there and the result is empty:

$ ./page_owner_sort dump.txt out.txt
loaded 0

Tell the two cases apart and report the record that does not fit, with the
name of the file, instead of silently returning an empty result.

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 | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tools/mm/page_owner_sort.c b/tools/mm/page_owner_sort.c
index 4c2a4e4..a042bf1 100644
--- a/tools/mm/page_owner_sort.c
+++ b/tools/mm/page_owner_sort.c
@@ -140,7 +140,7 @@ int read_block(char *buf, char *ext_buf, int
buf_size, FILE *fin)
return -1;
}

- return -1; /* no space left in buf. */
+ return -2; /* no space left in buf */
}

static int compare_txt(const void *p1, const void *p2)
@@ -794,7 +794,7 @@ int main(int argc, char **argv)
{
FILE *fin, *fout;
char *buf, *ext_buf;
- int i, count, compare_flag;
+ int i, count, compare_flag, ret = 0;
struct stat st;
int opt;
struct option longopts[] = {
@@ -965,6 +965,12 @@ int main(int argc, char **argv)
for ( ; ; ) {
int buf_len = read_block(buf, ext_buf, BUF_SIZE, fin);

+ if (buf_len < -1) {
+ fprintf(stderr, "record longer than %d bytes in %s\n",
+ BUF_SIZE, argv[optind]);
+ ret = 1;
+ goto out_free;
+ }
if (buf_len < 0)
break;
if (!add_list(buf, buf_len, ext_buf))
@@ -1050,5 +1056,5 @@ out_pid:
out_order:
regfree(&order_pattern);

- return 0;
+ return ret;
}
--
2.39.5