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

Linus Torvalds
2018-12-21T20:02:09Z

On Fri, Dec 21, 2018 at 11:40 AM Steven Rostedt <rostedt@goodmis.org> 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