public inbox for linux-arch@vger.kernel.org
 help / color / mirror / Atom feed
From: Chen Gang <gang.chen@asianux.com>
To: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Cc: David Howells <dhowells@redhat.com>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	Martin Schwidefsky <schwidefsky@de.ibm.com>,
	Heiko Carstens <heiko.carstens@de.ibm.com>,
	linux390@de.ibm.com, linux-s390@vger.kernel.org,
	Linux-Arch <linux-arch@vger.kernel.org>,
	"kernel-janitors@vger.kernel.org"
	<kernel-janitors@vger.kernel.org>,
	wharms@bfs.de
Subject: Re: [PATCH v2] arch: s390: appldata: using strncpy() and strnlen() instead of sprintf()
Date: Wed, 29 May 2013 09:40:55 +0800	[thread overview]
Message-ID: <51A55CA7.4040703@asianux.com> (raw)
In-Reply-To: <20130528180327.6e6171f0@thinkpad>

On 05/29/2013 12:03 AM, Gerald Schaefer wrote:
> On Tue, 28 May 2013 13:17:09 +0100
> David Howells <dhowells@redhat.com> wrote:
> 
>> > Chen Gang <gang.chen@asianux.com> wrote:
>> > 
>>> > > Your suggestion will improve the speed, but may merge "transferring
>>> > > 'protocol' data" and "processing 'protocol' data" together.
>> > 
>> > Look at it this way: You're having to step very carefully because you are
>> > fully expecting the strings not to be NUL-terminated.  Therefore you probably
>> > avoid using string functions if you can.
>> > 
>> > In fact, looking at the code, why are you copying the data through an
>> > intermediate buffer at all?  Why not just copy directly to userspace:
>> > 
>> > 	int len;
>> > 	char buf[2];
>> > 
>> > 	if (!*lenp || *ppos) {
>> > 		*lenp = 0;
>> > 		return 0;
>> > 	}
>> > 	if (!write) {
>> > -		len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
>> > +		const char *ptr = appldata_timer_active ? "1\n" : "0\n";
>> > +		size_t len = 2;
>> > 		if (len > *lenp)
>> > 			len = *lenp;
>> > 		if (copy_to_user(buffer, buf, len))
>> > 			return -EFAULT;
>> > 		goto out;
>> > 	}
>> > 
>> > Put like that, it's fairly obvious what is going on.
> Yes, we could do that for !write, but we can't get rid of the buffer
> completely, as we need it for the other case and copy_from_user().
> I have already applied the v2 version from Chen Gang, as it fixes the
> overflow bug, and the affected code is not performance critical at all.
> I like the improved readability of your approach, but the patch is already
> on its way to Martins "for-linus" branch, and I think it's "good enough".
> 
> Thanks for all the feedback to this little piece of very old (and partly
> ugly) code from my very first Linux device driver :-)
> 

OK, thank you. and thank all related feedback from Geert, Walter, David


Thanks.
-- 
Chen Gang

Asianux Corporation

  reply	other threads:[~2013-05-29  1:41 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-27  2:59 [PATCH] arch: s390: appldata: using strncpy() instead of sprintf() Chen Gang
2013-05-27  8:34 ` Geert Uytterhoeven
2013-05-27  9:06   ` Chen Gang
2013-05-27  9:43     ` [PATCH] arch: s390: include: asm: typo issue for the redundency comma, found by cross compiling Chen Gang
2013-05-27  9:55   ` [PATCH v2] arch: s390: appldata: using strncpy() and strnlen() instead of sprintf() Chen Gang
2013-05-27 16:23     ` Gerald Schaefer
2013-05-28  4:58       ` Chen Gang
2013-05-28 10:22     ` David Howells
2013-05-28 11:03       ` Chen Gang
2013-05-28 12:17         ` David Howells
2013-05-28 16:03           ` Gerald Schaefer
2013-05-29  1:40             ` Chen Gang [this message]
2013-05-29  1:28           ` Chen Gang
2013-05-29  6:30             ` Dan Carpenter
2013-05-29  7:29               ` Chen Gang
2013-05-29  7:47                 ` Chen Gang

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=51A55CA7.4040703@asianux.com \
    --to=gang.chen@asianux.com \
    --cc=dhowells@redhat.com \
    --cc=geert@linux-m68k.org \
    --cc=gerald.schaefer@de.ibm.com \
    --cc=heiko.carstens@de.ibm.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linux390@de.ibm.com \
    --cc=schwidefsky@de.ibm.com \
    --cc=wharms@bfs.de \
    /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