* [PATCH] lib: fix scnprintf() if @size is == 0
@ 2010-08-24 23:35 Changli Gao
2010-08-25 0:03 ` Joe Perches
0 siblings, 1 reply; 5+ messages in thread
From: Changli Gao @ 2010-08-24 23:35 UTC (permalink / raw)
To: akpm; +Cc: linux-kernel, Changli Gao
scnprintf() should return 0 if @size is == 0. Update the comment for it,
as @size is unsigned.
Signed-off-by: Changli Gao <xiaosuo@gmail.com>
---
lib/vsprintf.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 7af9d84..0c66ae9 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1497,7 +1497,7 @@ EXPORT_SYMBOL(snprintf);
* @...: Arguments for the format string
*
* The return value is the number of characters written into @buf not including
- * the trailing '\0'. If @size is <= 0 the function returns 0.
+ * the trailing '\0'. If @size is == 0 the function returns 0.
*/
int scnprintf(char *buf, size_t size, const char *fmt, ...)
@@ -1509,7 +1509,7 @@ int scnprintf(char *buf, size_t size, const char *fmt, ...)
i = vsnprintf(buf, size, fmt, args);
va_end(args);
- return (i >= size) ? (size - 1) : i;
+ return (i >= size) ? (size != 0 ? (size - 1) : 0) : i;
}
EXPORT_SYMBOL(scnprintf);
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] lib: fix scnprintf() if @size is == 0
2010-08-24 23:35 [PATCH] lib: fix scnprintf() if @size is == 0 Changli Gao
@ 2010-08-25 0:03 ` Joe Perches
2010-08-25 0:10 ` Changli Gao
0 siblings, 1 reply; 5+ messages in thread
From: Joe Perches @ 2010-08-25 0:03 UTC (permalink / raw)
To: Changli Gao; +Cc: akpm, linux-kernel
On Wed, 2010-08-25 at 07:35 +0800, Changli Gao wrote:
> scnprintf() should return 0 if @size is == 0. Update the comment for it,
> as @size is unsigned.
>
> Signed-off-by: Changli Gao <xiaosuo@gmail.com>
> ---
> lib/vsprintf.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index 7af9d84..0c66ae9 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -1497,7 +1497,7 @@ EXPORT_SYMBOL(snprintf);
> * @...: Arguments for the format string
> *
> * The return value is the number of characters written into @buf not including
> - * the trailing '\0'. If @size is <= 0 the function returns 0.
> + * the trailing '\0'. If @size is == 0 the function returns 0.
> */
>
> int scnprintf(char *buf, size_t size, const char *fmt, ...)
> @@ -1509,7 +1509,7 @@ int scnprintf(char *buf, size_t size, const char *fmt, ...)
> i = vsnprintf(buf, size, fmt, args);
> va_end(args);
>
> - return (i >= size) ? (size - 1) : i;
> + return (i >= size) ? (size != 0 ? (size - 1) : 0) : i;
Isn't simpler to check size at the beginning of the function?
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 7af9d84..779236f 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1505,6 +1505,9 @@ int scnprintf(char *buf, size_t size, const char *fmt, ...)
va_list args;
int i;
+ if (!size)
+ return 0;
+
va_start(args, fmt);
i = vsnprintf(buf, size, fmt, args);
va_end(args);
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] lib: fix scnprintf() if @size is == 0
2010-08-25 0:03 ` Joe Perches
@ 2010-08-25 0:10 ` Changli Gao
2010-08-25 0:35 ` Joe Perches
0 siblings, 1 reply; 5+ messages in thread
From: Changli Gao @ 2010-08-25 0:10 UTC (permalink / raw)
To: Joe Perches; +Cc: akpm, linux-kernel
On Wed, Aug 25, 2010 at 8:03 AM, Joe Perches <joe@perches.com> wrote:
>
> Isn't simpler to check size at the beginning of the function?
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index 7af9d84..779236f 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -1505,6 +1505,9 @@ int scnprintf(char *buf, size_t size, const char *fmt, ...)
> va_list args;
> int i;
>
> + if (!size)
> + return 0;
> +
It is a rare case, so don't slow down the normal users.
> va_start(args, fmt);
> i = vsnprintf(buf, size, fmt, args);
> va_end(args);
>
>
>
--
Regards,
Changli Gao(xiaosuo@gmail.com)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] lib: fix scnprintf() if @size is == 0
2010-08-25 0:10 ` Changli Gao
@ 2010-08-25 0:35 ` Joe Perches
2010-08-25 0:44 ` Changli Gao
0 siblings, 1 reply; 5+ messages in thread
From: Joe Perches @ 2010-08-25 0:35 UTC (permalink / raw)
To: Changli Gao; +Cc: akpm, linux-kernel
On Wed, 2010-08-25 at 08:10 +0800, Changli Gao wrote:
> On Wed, Aug 25, 2010 at 8:03 AM, Joe Perches <joe@perches.com> wrote:
> > Isn't simpler to check size at the beginning of the function?
> It is a rare case, so don't slow down the normal users.
That's fine, but I don't like blinking that much
when I read code.
How about:
if (i < size)
return i;
if (size)
return i - 1;
return 0;
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] lib: fix scnprintf() if @size is == 0
2010-08-25 0:35 ` Joe Perches
@ 2010-08-25 0:44 ` Changli Gao
0 siblings, 0 replies; 5+ messages in thread
From: Changli Gao @ 2010-08-25 0:44 UTC (permalink / raw)
To: Joe Perches; +Cc: akpm, linux-kernel
On Wed, Aug 25, 2010 at 8:35 AM, Joe Perches <joe@perches.com> wrote:
> On Wed, 2010-08-25 at 08:10 +0800, Changli Gao wrote:
>> On Wed, Aug 25, 2010 at 8:03 AM, Joe Perches <joe@perches.com> wrote:
>> > Isn't simpler to check size at the beginning of the function?
>> It is a rare case, so don't slow down the normal users.
>
> That's fine, but I don't like blinking that much
> when I read code.
>
"? :" isn't friendly. OK, I'll update my patch. Thanks.
--
Regards,
Changli Gao(xiaosuo@gmail.com)
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-08-25 0:44 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-24 23:35 [PATCH] lib: fix scnprintf() if @size is == 0 Changli Gao
2010-08-25 0:03 ` Joe Perches
2010-08-25 0:10 ` Changli Gao
2010-08-25 0:35 ` Joe Perches
2010-08-25 0:44 ` Changli Gao
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®