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

From: Alejandro Colomar

Date: Sat Nov 14 2026 - 12:03:54 EST


Hi Joe,

> Date: 2026-11-14 07:28:23-0800
> From: Joe Perches <joe@xxxxxxxxxxx>
>
> 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.
>
> I still think this description is poor.
> It's neither a bug nor a flaw.
> It's a design choice.

As explained in more detail in the subthread, I think it is a bug.

If the function name were strprefix_len(), it would be a legitimate
design choice, since 0 would indeed be the length of such a prefix, and
it would be arguably useful to return 0 on a non-matching prefix.

But the function is named str_has_prefix(). Since an empty string is by
convention (strstr(3), etc.) the prefix of every string, returning false
is a bug. Since the function returns 0 for such a case, it misbehaves.

Perhaps we can reach an agreement if I prepend this commit with one that
renames all uses of str_has_prefix() to strprefix_len(). Then, I would
agree to consider this as a design choice, and remove the text above.


Cheers,
Alex

--
<https://www.alejandro-colomar.es>

Attachment: signature.asc
Description: PGP signature