mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg KH <greg@kroah.com>
To: "Németh Márton" <nm127@freemail.hu>
Cc: Greg Kroah-Hartman <gregkh@suse.de>,
	Matt Mooney <mfm@muteddisk.com>,
	Kulikov Vasiliy <segooon@gmail.com>,
	Endre Kollar <taxy443@gmail.com>, Arjan Mels <arjan.mels@gmx.net>,
	Ilia Mirkin <imirkin@alum.mit.edu>,
	David Chang <dchang@novell.com>,
	Himanshu Chauhan <hschauhan@nulltrace.org>,
	Max Vozeler <max@vozeler.com>, Arnd Bergmann <arnd@arndb.de>,
	usbip-devel@lists.sourceforge.net, devel@driverdev.osuosl.org,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] usbip: handle length at sysfs show() functions
Date: Tue, 7 Jun 2011 14:34:18 -0700	[thread overview]
Message-ID: <20110607213418.GB11102@kroah.com> (raw)
In-Reply-To: <4DE5CA9F.6010303@freemail.hu>

On Wed, Jun 01, 2011 at 07:14:07AM +0200, Németh Márton wrote:
> From: Márton Németh <nm127@freemail.hu>
> 
> The sysfs show() functions shall return the actual content length of
> the result buffer. According to Documentation/filesystems/sysfs.txt:215
> the scnprintf() function is preferred.
> 
> See also the article titled "snprintf() confusion" at
> http://lwn.net/Articles/69419/ .
> 
> Signed-off-by: Márton Németh <nm127@freemail.hu>
> ---
> diff --git a/drivers/staging/usbip/stub_dev.c b/drivers/staging/usbip/stub_dev.c
> index 6e99ec8..af5f107 100644
> --- a/drivers/staging/usbip/stub_dev.c
> +++ b/drivers/staging/usbip/stub_dev.c
> @@ -80,7 +80,7 @@ static ssize_t show_status(struct device *dev, struct device_attribute *attr,
>  	status = sdev->ud.status;
>  	spin_unlock(&sdev->ud.lock);
> 
> -	return snprintf(buf, PAGE_SIZE, "%d\n", status);
> +	return scnprintf(buf, PAGE_SIZE, "%d\n", status);

Actually, this can be changed to just be:
	return sprintf(buf, "%d\n", status);
as obviously an integer will never be bigger than the sysfs buffer.

Good rule of thumb, if you care about the size of the sysfs buffer, you
are doing something wrong.

Case in point:

> --- a/drivers/staging/usbip/stub_main.c
> +++ b/drivers/staging/usbip/stub_main.c
> @@ -79,18 +79,22 @@ static ssize_t show_match_busid(struct device_driver *drv, char *buf)
>  {
>  	int i;
>  	char *out = buf;
> +	int count = 0;
> 
>  	spin_lock(&busid_table_lock);
> 
>  	for (i = 0; i < MAX_BUSID; i++)
> -		if (busid_table[i].name[0])
> -			out += sprintf(out, "%s ", busid_table[i].name);
> +		if (busid_table[i].name[0]) {
> +			count += scnprintf(out, PAGE_SIZE - count,
> +					"%s ", busid_table[i].name);
> +			out = buf + count;
> +		}

Here we are doing lots of work to try to put more than one value in the
sysfs file, and return the proper data to the kernel about how big the
buffer we used.

That's wrong, and violates the "one value per file" sysfs rule, so that
should be fixed instead of trying to change the sprintf() call.

Same goes for other places in this patch.

Care to redo that instead?

thanks,

greg k-h

  parent reply	other threads:[~2011-06-07 21:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-01  5:14 Németh Márton
2011-06-07 21:31 ` Greg KH
2011-06-07 21:34 ` Greg KH [this message]
2011-06-08  5:26   ` Németh Márton
2011-06-08 16:09     ` Greg KH
2011-06-08 21:27       ` Németh Márton
2011-06-08 22:16         ` Greg KH
     [not found]           ` <BANLkTikokYys2srX3+5r+fkdaaX5V2zxUg@mail.gmail.com>
2011-06-29  2:45             ` matt mooney
2011-06-29  5:54               ` Németh Márton
2011-06-29 16:20               ` Matt Chen
2011-06-29  2:23         ` matt mooney

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=20110607213418.GB11102@kroah.com \
    --to=greg@kroah.com \
    --cc=arjan.mels@gmx.net \
    --cc=arnd@arndb.de \
    --cc=dchang@novell.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=gregkh@suse.de \
    --cc=hschauhan@nulltrace.org \
    --cc=imirkin@alum.mit.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=max@vozeler.com \
    --cc=mfm@muteddisk.com \
    --cc=nm127@freemail.hu \
    --cc=segooon@gmail.com \
    --cc=taxy443@gmail.com \
    --cc=usbip-devel@lists.sourceforge.net \
    /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®