All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Kees Cook <keescook@chromium.org>
Cc: Denis Efremov <efremov@linux.com>,
	Julia Lawall <julia.lawall@inria.fr>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-usb@vger.kernel.org,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	linux-kernel@vger.kernel.org, cocci <cocci@systeme.lip6.fr>,
	Alex Dewar <alex.dewar90@gmail.com>
Subject: Re: [Cocci] [PATCH] usb: atm: don't use snprintf() for sysfs attrs
Date: Fri, 28 Aug 2020 01:22:23 -0700	[thread overview]
Message-ID: <a98b0a411abdaf6b58c73322511087f57353fb22.camel@perches.com> (raw)
In-Reply-To: <526af204ddf95f94012c6132d12693852bfe7442.camel@perches.com>

On Fri, 2020-08-28 at 01:10 -0700, Joe Perches wrote:
> On Fri, 2020-08-28 at 00:58 -0700, Kees Cook wrote:
> > On Thu, Aug 27, 2020 at 09:12:06PM -0700, Joe Perches wrote:
> > > Perhaps something like the below with a sample conversion
> > > that uses single and multiple sysfs_emit uses.
> > 
> > On quick review, I like it. :)
> > 
> > > [...]
> > > +int sysfs_emit(char *buf, char *pos, const char *fmt, ...)
> > > +{
> > > +	int len;
> > > +	va_list args;
> > > +
> > > +	WARN(pos < buf, "pos < buf\n");
> > > +	WARN(pos - buf >= PAGE_SIZE, "pos >= PAGE_SIZE (%tu > %lu)\n",
> > > +	     pos - buf, PAGE_SIZE);
> > > +	if (pos < buf || pos - buf >= PAGE_SIZE)
> > > +		return 0;
> > 
> > This can be:
> > 
> > 	if (WARN(pos < buf, "pos < buf\n") ||
> > 	    WARN(pos - buf >= PAGE_SIZE, "pos >= PAGE_SIZE (%tu > %lu)\n",
> > 		 pos - buf, PAGE_SIZE))
> > 		return 0;
> 
> I have some vague recollection that WARN could be compiled
> away to nothing somehow.  True or false?
> 
> If false, sure, of course, it'd be faster too.

I can't find an instance where WARN doesn't return the
condition.

And likely even faster would be to just show "invalid pos"
instead of specific messages.

	if (WARN(pos < buf || (pos - buf) >= PAGE_SIZE,
		 "Invalid pos\n");
		return 0;

or maybe use WARN_ONCE or no WARN at all.


WARNING: multiple messages have this Message-ID (diff)
From: Joe Perches <joe@perches.com>
To: Kees Cook <keescook@chromium.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-usb@vger.kernel.org,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-kernel@vger.kernel.org, Alex Dewar <alex.dewar90@gmail.com>,
	cocci <cocci@systeme.lip6.fr>
Subject: Re: [Cocci] [PATCH] usb: atm: don't use snprintf() for sysfs attrs
Date: Fri, 28 Aug 2020 01:22:23 -0700	[thread overview]
Message-ID: <a98b0a411abdaf6b58c73322511087f57353fb22.camel@perches.com> (raw)
In-Reply-To: <526af204ddf95f94012c6132d12693852bfe7442.camel@perches.com>

On Fri, 2020-08-28 at 01:10 -0700, Joe Perches wrote:
> On Fri, 2020-08-28 at 00:58 -0700, Kees Cook wrote:
> > On Thu, Aug 27, 2020 at 09:12:06PM -0700, Joe Perches wrote:
> > > Perhaps something like the below with a sample conversion
> > > that uses single and multiple sysfs_emit uses.
> > 
> > On quick review, I like it. :)
> > 
> > > [...]
> > > +int sysfs_emit(char *buf, char *pos, const char *fmt, ...)
> > > +{
> > > +	int len;
> > > +	va_list args;
> > > +
> > > +	WARN(pos < buf, "pos < buf\n");
> > > +	WARN(pos - buf >= PAGE_SIZE, "pos >= PAGE_SIZE (%tu > %lu)\n",
> > > +	     pos - buf, PAGE_SIZE);
> > > +	if (pos < buf || pos - buf >= PAGE_SIZE)
> > > +		return 0;
> > 
> > This can be:
> > 
> > 	if (WARN(pos < buf, "pos < buf\n") ||
> > 	    WARN(pos - buf >= PAGE_SIZE, "pos >= PAGE_SIZE (%tu > %lu)\n",
> > 		 pos - buf, PAGE_SIZE))
> > 		return 0;
> 
> I have some vague recollection that WARN could be compiled
> away to nothing somehow.  True or false?
> 
> If false, sure, of course, it'd be faster too.

I can't find an instance where WARN doesn't return the
condition.

And likely even faster would be to just show "invalid pos"
instead of specific messages.

	if (WARN(pos < buf || (pos - buf) >= PAGE_SIZE,
		 "Invalid pos\n");
		return 0;

or maybe use WARN_ONCE or no WARN at all.

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

  reply	other threads:[~2020-08-28  8:22 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-24 22:23 [PATCH] usb: atm: don't use snprintf() for sysfs attrs Alex Dewar
2020-08-25  8:12 ` David Laight
2020-08-25  8:17   ` Alex Dewar
2020-08-25  9:07     ` David Laight
2020-08-25  8:24   ` Greg Kroah-Hartman
2020-08-27 16:49     ` Kees Cook
2020-08-27 17:45       ` Greg Kroah-Hartman
2020-08-27  6:42 ` Rasmus Villemoes
2020-08-27  7:15   ` Greg Kroah-Hartman
2020-08-27 13:18     ` Alex Dewar
2020-08-27 13:41       ` Rasmus Villemoes
2020-08-27 14:48         ` Alex Dewar
2020-08-27 16:58           ` Joe Perches
2020-08-27 16:58             ` [Cocci] " Joe Perches
2020-08-27 19:42             ` Julia Lawall
2020-08-27 19:42               ` Julia Lawall
2020-08-27 20:29               ` Joe Perches
2020-08-27 20:29                 ` Joe Perches
2020-08-27 21:00                 ` Joe Perches
2020-08-27 21:00                   ` Joe Perches
2020-08-27 21:29                 ` Julia Lawall
2020-08-27 21:29                   ` Julia Lawall
2020-08-27 22:03                 ` David Laight
2020-08-27 22:03                   ` David Laight
2020-08-27 22:11                   ` Joe Perches
2020-08-27 22:11                     ` Joe Perches
2020-08-27 22:16                     ` Kees Cook
2020-08-27 22:16                       ` Kees Cook
2020-08-27 21:01               ` Denis Efremov
2020-08-27 21:01                 ` Denis Efremov
2020-08-27 21:36                 ` Julia Lawall
2020-08-27 21:36                   ` Julia Lawall
2020-08-27 21:44                   ` Joe Perches
2020-08-27 21:44                     ` Joe Perches
2020-08-27 22:38                     ` Denis Efremov
2020-08-27 22:38                       ` Denis Efremov
2020-08-27 22:48                       ` Joe Perches
2020-08-27 22:48                         ` Joe Perches
2020-08-27 22:20                 ` Kees Cook
2020-08-27 22:20                   ` Kees Cook
2020-08-27 22:45                   ` Joe Perches
2020-08-27 22:45                     ` Joe Perches
2020-08-28  4:12                     ` Joe Perches
2020-08-28  4:12                       ` Joe Perches
2020-08-28  7:58                       ` Kees Cook
2020-08-28  7:58                         ` Kees Cook
2020-08-28  8:10                         ` Joe Perches
2020-08-28  8:10                           ` Joe Perches
2020-08-28  8:22                           ` Joe Perches [this message]
2020-08-28  8:22                             ` Joe Perches
2020-08-28  7:39                   ` David Laight
2020-08-28  7:39                     ` David Laight
2020-08-27 21:54             ` David Laight
2020-08-27 21:54               ` [Cocci] " David Laight

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=a98b0a411abdaf6b58c73322511087f57353fb22.camel@perches.com \
    --to=joe@perches.com \
    --cc=alex.dewar90@gmail.com \
    --cc=cocci@systeme.lip6.fr \
    --cc=efremov@linux.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=gustavoars@kernel.org \
    --cc=julia.lawall@inria.fr \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.