Re: [PATCH v5 4/6] selftests/mm/vm_util: robust write_file()

Andrew Morton
2026-03-23T22:39:46Z

On Mon, 23 Mar 2026 23:17:51 +0800 Chunyu Hu <chuhu@redhat.com> wrote:

> Add two more checks for buflen and numwritten. The buflen should be at
> least one, otherwise the 'buflen - 1' could underflow and cause trouble.
> The numwritten should be equal to 'buflen - 1'. The test will exit if
> any of these conditions aren't met.
>
> Additionally, add more print information when a write failure occurs or
> a truncated write happens, providing clearer diagnostics.
>
> ...
>
> --- a/tools/testing/selftests/mm/vm_util.c
> +++ b/tools/testing/selftests/mm/vm_util.c
> @@ -769,6 +769,8 @@ void write_file(const char *path, const char *buf, size_t buflen)
> {
> int fd;
> ssize_t numwritten;
> + if (buflen < 1)
> + ksft_exit_fail_msg("Incorrect buffer len: %zu\n", buflen);
>
> fd = open(path, O_WRONLY);
> if (fd == -1)
> @@ -777,5 +779,9 @@ void write_file(const char *path, const char *buf, size_t buflen)
> numwritten = write(fd, buf, buflen - 1);
> close(fd);
> if (numwritten < 1)
> - ksft_exit_fail_msg("Write failed\n");
> + ksft_exit_fail_msg("%s write(%s) failed: %s\n", path, buf,
> + strerror(errno));

AI review
(https://sashiko.dev/#/patchset/20260323151753.2576137-1-chuhu@redhat.com)
points out that `errno' was overwritten by the close(). Maybe. Or
maybe a successful close() leaves errno alone, dunno.

Apart from that the comments appear fairly minor. Please lmk if you
think we should proceed as-is.

(I'm really trying to slow things down now - we have a *lot* of
material and a few weeks of consolidation is needed. But selftests/
tend to get a pass, for obvious reasons)