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

From: Linus Torvalds
Date: Fri Dec 21 2018 - 15:02:16 EST


On Fri, Dec 21, 2018 at 11:40 AM Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:
>
> OK, what about if we just use strlen() and say that this macro is not
> safe for parameters with side effects.

I think gcc follows simple assignments just fine, and does the
optimized strlen() for them too.

So why not just do

#define have_prefix(str,prefix) ({ \
const char *__pfx = prefix; \
size_t __pfxlen = strlen(__pfx); \
strncmp(str, __pfx, __pfxlen) ? 0 : __pfxlen); })

and be done with it safely?

The above is ENTIRELY untested.

Linus