Re: [PATCH] selftests/mm/pagemap_ioctl: Fix missing NULL checks after calloc()

From: SJ Park

Date: Tue Jul 21 2026 - 22:31:57 EST


Hello longlong,


I found get_maitnainer.pl is suggesting to Cc below recipients.

- Lorenzo Stoakes <ljs@xxxxxxxxxx>
- "Liam R. Howlett" <liam@xxxxxxxxxxxxx>
- Vlastimil Babka <vbabka@xxxxxxxxxx>
- Mike Rapoport <rppt@xxxxxxxxxx>
- Suren Baghdasaryan <surenb@xxxxxxxxxx>
- Michal Hocko <mhocko@xxxxxxxx>

On Tue, 21 Jul 2026 14:36:11 +0800 longlong yan <yanlonglong@xxxxxxxxxx> wrote:

> The pagemap_ioctl selftest allocates memory via calloc() in several
> places but does not check the return values. If calloc() fails, the
> subsequent code will dereference a NULL pointer and crash.
>
> Additionally, in sanity_tests(), the calloc() failure check incorrectly
> uses MAP_FAILED (the mmap() error constant) instead of NULL. Since
> calloc() returns NULL on failure, the check never triggers and a
> failed allocation goes undetected.
>
> Add NULL checks after each calloc() call, and fix the wrong error
> constant in sanity_tests(). Use ksft_exit_fail_msg() consistent with
> the existing error handling pattern in the file.

Makes sense to me.

>
> Signed-off-by: longlong yan <yanlonglong@xxxxxxxxxx>

I have a trivial comment below. Regardless of that,

Reviewed-by: SJ Park <sj@xxxxxxxxxx>

> ---
> tools/testing/selftests/mm/pagemap_ioctl.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
[...]
> @@ -1000,6 +1004,8 @@ int unmapped_region_tests(void)
> int written, len = 0x00040000;
> long vec_size = len / page_size;
> struct page_region *vec = calloc(vec_size, sizeof(struct page_region));
> + if (!vec)
> + ksft_exit_fail_msg("error nomem\n");

I'd suggest to separate the definitions and statements by putting an empty line
in the middle.


Thanks,
SJ

[...]