Re: [PATCH] selftests/mm: check strdup() and fix buf leak in parse_test_type()
From: David Hildenbrand (Arm)
Date: Fri Aug 21 2026 - 10:25:02 EST
On 8/21/26 13:44, Anshuman wrote:
> The return value of strdup() is never checked before being passed to
> strsep() and strcmp(). If strdup() fails and returns NULL, strsep()
> returns NULL as well, and the subsequent strcmp(NULL, "all") is
> undefined behavior, likely causing a crash.
In practice this is extraordinarily unlikely to ever fail. :)
So I don't think we would ever experience this.
>
> Additionally, buf is never freed. strsep() advances the buf pointer
> past the first token, so by the time buf would normally be freed,
> the original pointer returned by strdup() has already been
> overwritten and is no longer available.
Given that parse_test_type() is called only once, nobody cares.
>
> Check strdup()'s return value and fail cleanly on allocation failure.
> Keep a separate pointer to the original allocation so it can be
> freed once buf is done being used, after all parsing has completed
> successfully.
>
> Signed-off-by: Anshuman <anshumantewari123@xxxxxxxxx>
> ---
[...]
That's too much churn for something that is irrelevant in practice and
makes the code more complicated.
So the following is better I think: