Re: [for-next][PATCH 23/24] string.h: Add strncmp_prefix() helper macro

Steven Rostedt
2018-12-23T22:56:26Z

On Sun, 23 Dec 2018 23:01:52 +0100
Rasmus Villemoes <linux@rasmusvillemoes.dk> wrote:

> On 21/12/2018 23.20, Joe Perches wrote:
> > On Fri, 2018-12-21 at 16:08 -0500, Steven Rostedt wrote:
> >> On Fri, 21 Dec 2018 21:58:32 +0100
> >> Andreas Schwab <schwab@linux-m68k.org> wrote:
> >>
> >>
> >>>> Well, perhaps I can just remove the ending ones. I get paranoid with
> >>>> macro variables, and tend to over do it so that there's no question.
> >>>
> >>> Why not make it an inline function?
> >>
> >> Matters if that removes the strlen(const) optimization. I could try it
> >> and see what happens.
> >
> > Using
> >
> > static inline bool str_has_prefix(const char *str, const char prefix[])
> > {
> > return !strncmp(str, prefix, strlen(prefix));
> > }
> >
>
> We already have exactly that function, it's called strstarts().

It's not exact.

>
> commit 66f92cf9d415e96a5bdd6c64de8dd8418595d2fc
> Author: Rusty Russell <rusty@rustcorp.com.au>
> Date: Tue Mar 31 13:05:36 2009 -0600
>
> strstarts: helper function for !strncmp(str, prefix, strlen(prefix))
>
> Please don't add a copy under another name.
>
> As for converting existing users, go for it. FWIW, I ran a cocci script
> a few years ago to find suspicious strncmp() cases, and there were some
> (e87c3f, ca957b6), but fewer than I expected. There are some
> confused/confusing ones that apparently deliberately do strncmp(a, b,
> sizeof(b)) instead of the equivalent to strcmp(a, b) (e.g. 'strncmp(str,
> "hwc", 4) == 0')

Well, one thing that str_has_prefix() does that strstarts() does not,
is to return the prefix length which gets rid of the duplication.

Would it be OK to convert strstarts() to return the length of prefix?

-- Steve