mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Joe Perches <joe@perches.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Ingo Molnar <mingo@kernel.org>, Tom Zanussi <zanussi@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>
Subject: Re: [RFC][PATCH] string.h: Add strncmp_prefix() helper macro
Date: Wed, 19 Dec 2018 20:43:39 -0500	[thread overview]
Message-ID: <20181220014339.f32q6duwein53bfd@home.goodmis.org> (raw)
In-Reply-To: <b6cc5327509148fb26bfba2ca34f9c2927437707.camel@perches.com>

[ Sending now as myself and not my daughter :-p ]

On Wed, Dec 19, 2018 at 01:54:57PM -0800, Joe Perches wrote:
> On Wed, 2018-12-19 at 16:32 -0500, Bryana Rostedt wrote:
> >  
> > +/*
> > + * A common way to test a prefix of a string is to do:
> > + *  strncmp(str, prefix, sizeof(prefix) - 1)
> > + *
> > + * But this can lead to bugs due to typos, or if prefix is a pointer
> > + * and not a constant. Instead use strncmp_prefix().
> > + */
> > +#define strncmp_prefix(str, prefix)					\e
> > +	({								\
> > +		int ____strcmp_prefix_ret____;				\
> > +		char *____strcmp_prefix____ = prefix;			\
> 
> This creates a warning and discards any const from prefix when used like
> 
> 	static const char foo[] = "bar";
> 
> 	strncmp_prefix(str, foo);
> 
> > +		if (__builtin_constant_p(&prefix))			\
> > +			____strcmp_prefix_ret____ =			\
> > +				strncmp(str, prefix, sizeof(prefix) - 1); \
> > +		else							\
> > +			____strcmp_prefix_ret____ =			\
> > +				strncmp(str, ____strcmp_prefix____,	\
> > +					strlen(____strcmp_prefix____));	\
> > +		____strcmp_prefix_ret____;				\
> > +	})
> > +
> 
> Perhaps
> 
> #define strncmp_prefix(str, prefix)					\
> ({									\
> 	typeof(prefix) p = (prefix);					\
> 	strncmp(str, p, strlen(p));					\


I'll have to see how all supported  gcc optimizes that.

I could just move the prefix assignment in the else part, which gets compiled
out on constants anyway, just to be safe, and also shows exactly what is
happening and not worrying about gcc doing the "right" thing.

-- Steve


> })
> 

  parent reply	other threads:[~2018-12-20  1:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-19 21:32 Bryana Rostedt
2018-12-19 21:54 ` Joe Perches
2018-12-19 22:21   ` Steven Rostedt
2018-12-20  1:43   ` Steven Rostedt [this message]
2018-12-19 22:05 ` Joe Perches
2018-12-20  1:46   ` Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181220014339.f32q6duwein53bfd@home.goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=zanussi@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®