Re: [PATCH v6 02/19] string: Add strprefix()

From: Joe Perches

Date: Sat Nov 14 2026 - 10:53:58 EST


On Sat, 2026-11-14 at 16:22 +0100, Alejandro Colomar wrote:
> As Bartosz reported, str_has_prefix() has a bug: it can't differentiate
> between an empty prefix matching an empty string from a non-matching
> prefix. This is a fundamental design flaw in this API, and not
> something that can be fixed.
>
> As always, this is a consequence of choosing sizes instead of pointers.
> Sizes are often the wrong choice. In this specific case, pointers are
> more capable of differentiating these cases, as NULL is a special
> sentinel value that doesn't need to overload a valid value.
>
> Let's implement strprefix(), a function that returns a pointer
> right after the prefix if found, and a null pointer if not found.
>
> We'll slowly migrate existing code from str_has_prefix() to strprefix().
>
> Move the description from str_has_prefix(), and describe
> str_has_prefix() by comparing it to strprefix().
>
> Closes: https://lore.kernel.org/linux-hardening/20261104-fwnode-for-each-prop-v1-1-6e28fe1c72c4@xxxxxxxxxxxxxxxx/
> Reported-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>
> Signed-off-by: Alejandro Colomar <alx@xxxxxxxxxx>
> ---
> include/linux/string.h | 29 ++++++++++++++++++++++++-----
> lib/tests/string_kunit.c | 9 +++++++++
> 2 files changed, 33 insertions(+), 5 deletions(-)
>
> diff --git a/include/linux/string.h b/include/linux/string.h
> index 9b3e578d9aca..14e825e902eb 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -557,16 +557,35 @@ void memcpy_and_pad(void *dest, size_t dest_len, const void *src, size_t count,
> sizeof(*(obj)) - offsetof(typeof(*(obj)), member)); \
> })
>
> +/**
> + * strprefix - string prefix
> + * @s: The string to be tested
> + * @prefix: The string to see if @s starts with
> + *
> + * A common way to test a prefix of a string is to do:
> + * strncmp(s, prefix, sizeof(prefix) - 1)
> + *
> + * But this can lead to bugs due to typos, or if prefix is a pointer
> + * and not a constant. Instead use strprefix().
> + *
> + * Returns:
> + * * @s + strlen(@prefix) if @s starts with @prefix
> + * * NULL if @s does not start with @prefix
> + */
> +__attribute__((nonnull(1, 2)))

__nonnull_args(1, 2)

A negative of this is that it implies the return should
always be tested even though it can be known that the
return is effectively bool.

Also the block

> + * A common way to test a prefix of a string is to do:
> + * strncmp(s, prefix, sizeof(prefix) - 1)

IMO isn't useful and the description doesn't say what happens
when prefix is an empty string (which IMO should match).

And the name strprefix could be confused with a function
that actually prefixes a string to another string.

maybe strprefixed or str_is_prefixed ?

And these are all candidates for converted uses to
whatever is the function name or steq:

$ git grep -P '[^w]strncmp\s*\(\s*[\w\.\-\>\s]*,\s*\"\w*"\s*,\s*\d+\s*\)' -- '*.[ch]' | wc -l
946

[^w] because of prom_strncmp