Re: [PATCH v7 0/4] Add strprefix() and fix a bug with it
From: Bartosz Golaszewski
Date: Thu Nov 26 2026 - 04:30:29 EST
On Wed, Nov 25, 2026 at 10:03:21PM +0100, Alejandro Colomar wrote:
>
> Hi!
>
> str_has_prefix() is based on sizes and lengths, which means it must
> overload the valid length 0 to mean not found. This is a typical
> consequence of choosing to design an API around sizes and lengths, and
> vanishes when moving to pointer-based similar APIs.
>
> According to its author (Steven R.), str_has_prefix() was never meant to
> accept an empty prefix, but that's not documented, and there's no
> obvious reason why that would be acceptable for a generic string
> function (as opposed to a function very specific to its original use).
> Anyway, as suggested by Steven, I've added a WARN_ON_ONCE() to catch
> such uses, and also added documentation stating this limitation.
>
> If the function had been named something like strprefix_len(), that
> design would have made sense, but being called str_has_prefix(), the
> obvious way to call it will result in bugs, because the name contradicts
> its intended use.
>
> Users of strings need a function whose behavior extends naturally to the
> empty string, and we can do better, by returning a pointer. And indeed,
> users have ended up using str_has_prefix() with empty prefix, expecting
> it to work (as is natural of a generic string API with that name).
>
> This patch set adds strprefix(), and uses strprefix() in one place to
> fix a bug reported by Bartosz.
>
Tested-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxxxxxxxx>