From: Chen Gang <gang.chen@asianux.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
Rusty Russell <rusty@rustcorp.com.au>, Robin Holt <holt@sgi.com>,
athorlton@sgi.com, Al Viro <viro@zeniv.linux.org.uk>,
linux-next@vger.kernel.org
Subject: Re: [PATCH] kernel/panic.c: reduce 1 byte usage for print tainted buffer.
Date: Tue, 08 Oct 2013 09:10:37 +0800 [thread overview]
Message-ID: <52535B8D.5000200@asianux.com> (raw)
In-Reply-To: <20131007172745.4f023e312df95bd94f078ca8@linux-foundation.org>
On 10/08/2013 08:27 AM, Andrew Morton wrote:
> On Sat, 05 Oct 2013 23:50:39 +0800 Chen Gang <gang.chen@asianux.com> wrote:
>
>> sizeof("Tainted: ") already counts '\0', and after first sprintf(), 's'
>> will start from the current string end (its' value is '\0').
>>
>> So need not add additional 1 byte for maximized usage of 'buf' in
>> print_tainted().
>>
>>
>> Signed-off-by: Chen Gang <gang.chen@asianux.com>
>> ---
>> kernel/panic.c | 2 +-
>> 1 files changed, 1 insertions(+), 1 deletions(-)
>>
>> diff --git a/kernel/panic.c b/kernel/panic.c
>> index b6c482c..c00b4ce 100644
>> --- a/kernel/panic.c
>> +++ b/kernel/panic.c
>> @@ -233,7 +233,7 @@ static const struct tnt tnts[] = {
>> */
>> const char *print_tainted(void)
>> {
>> - static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ") + 1];
>> + static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
>>
>> if (tainted_mask) {
>> char *s;
>
> hm, that code is a bit crufty.
>
> - Why is `buf' static? It could be on-stack.
>
Just like Al Viro's reply ('buf' will be returned to caller).
> - Requires that the two literal "Tainted: " strings be kept in sync.
>
Hmm... if more than 2, we should use a macro instead of, else (within
2), especially they are near by, we can still let it hard coded, I feel
that will more straightful for readers and writers.
> - Assumes that strlen("Not tainted") <= strlen("Tainted") +
> ARRAY_SIZE(tnts). Which is true, but might not be if someone makes
> changes...
>
Hmm... it use snprintf() instead of sprintf(), although I feel better
using scnprintf() instead of.
This string can be trucated, and scnprintf() is more suitable for this
kind of string. And snprintf() is for the string which can not be
truncated (will return the ideal length to notify the caller).
>
>
>
Thanks.
--
Chen Gang
next prev parent reply other threads:[~2013-10-08 1:11 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-05 15:50 [PATCH] kernel/panic.c: reduce 1 byte usage for print tainted buffer Chen Gang
2013-10-05 15:53 ` Chen Gang
2013-10-07 16:35 ` Alex Thorlton
2013-10-07 21:04 ` Chen Gang
2013-10-08 0:27 ` Andrew Morton
2013-10-08 0:32 ` Al Viro
2013-10-08 1:10 ` Chen Gang [this message]
2013-10-08 1:25 ` Andrew Morton
2013-10-08 1:45 ` 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=52535B8D.5000200@asianux.com \
--to=gang.chen@asianux.com \
--cc=akpm@linux-foundation.org \
--cc=athorlton@sgi.com \
--cc=holt@sgi.com \
--cc=linux-next@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=rusty@rustcorp.com.au \
--cc=viro@zeniv.linux.org.uk \
/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.