Skip to contentSkip to conversation

lib: fix scnprintf() if @size is == 0

PATCH Superseded Changli Gao 2 participants 5 messages Aug 24, 2010 – Aug 25, 2010

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: fix scnprintf() if @size is == 0

scnprintf() should return 0 if @size is == 0. Update the comment for it, as @size is unsigned.

---
 lib/vsprintf.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/vsprintf.c +2 -2
index 7af9d84..0c66ae9 100644
@@ -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);
 
#1 lore raw
Quoted 21 lines · Changli Gao

scnprintf() should return 0 if @size is == 0. Update the comment for it, as @size is unsigned.

---
 lib/vsprintf.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/vsprintf.c +1 -1
index 7af9d84..0c66ae9 100644
@@ -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, ...)

lib/vsprintf.c int scnprintf(char *buf, size_t size, const char *fmt, ...) · line 1512

Quoted 5 lines
lib/vsprintf.c +1 -1
@@ -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?

lib/vsprintf.c +3 -0
index 7af9d84..779236f 100644
@@ -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);
#2 lore raw
Quoted 8 lines · Joe Perches

Isn't simpler to check size at the beginning of the function?

lib/vsprintf.c +0 -0
index 7af9d84..779236f 100644

lib/vsprintf.c int scnprintf(char *buf, size_t size, const char *fmt, ...) · line 1510

Quoted 6 lines
lib/vsprintf.c +3 -0
@@ -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.

Quoted 6 lines
lib/vsprintf.c +0 -0
        va_start(args, fmt);
        i = vsnprintf(buf, size, fmt, args);
        va_end(args);
Signature · 2 lines
Regards,
Changli Gao([email protected])
#3 lore raw
Quoted 3 lines · Changli Gao
Quoted 1 line · Joe Perches

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;
#4 lore raw
Quoted 8 lines · Joe Perches
Quoted 3 lines · Changli Gao
Quoted 1 line · Joe Perches

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.

Signature · 2 lines
Regards,
Changli Gao([email protected])