Re: [PATCH v3 1/5] string: Add strprefix(), and deprecate str_has_prefix()
From: Alejandro Colomar
Date: Tue Nov 10 2026 - 11:23:22 EST
Hi Joe,
> Date: 2026-11-10 07:47:55-0800
> From: Joe Perches <joe@xxxxxxxxxxx>
>
> On Tue, 2026-11-10 at 16:33 +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().
>
> Most uses of str_has_prefix could use the existing strstarts.
Hmmm, actually, both str_has_prefix() and strstarts() can be replaced by
strprefix(), which provides functionality for both use cases. It's the
more generic API for this. And I expect compilers can optimize the
unused parts if actually unused.
Have a lovely day!
Alex
--
<https://www.alejandro-colomar.es>
Attachment:
signature.asc
Description: PGP signature