Re: [PATCH v2] tools/mm: prevent page_owner_sort from truncating input
From: Andrew Morton
Date: Thu Jul 30 2026 - 16:45:44 EST
On Thu, 30 Jul 2026 09:58:09 +0800 Warren Xiong <warren.xiong@xxxxxxxxxx> wrote:
> page_owner_sort opens the output file with "w" before reading the input.
> If both paths refer to the same file, this truncates the input and the
> tool silently processes zero records before returning success.
>
> Delay opening the output file until all input records have been loaded
> into memory. This allows the tool to sort a file in place without
> truncating data before it has been consumed.
Far nicer, thanks.
However I spotted a thing which Sashiko missed!
> @@ -874,6 +873,13 @@ int main(int argc, char **argv)
> goto out_free;
> }
>
> + fout = fopen(argv[optind + 1], "w");
> + if (!fout) {
> + usage();
> + perror("open: ");
> + exit(1);
> + }
usage() calls printf(), which can set errno. So perror() might print
the wrong thing.
Not that I care very much ;)