Skip to content

Commit

Permalink
vsprintf: fix bug in negative value printing
Browse files Browse the repository at this point in the history
Sitsofe Wheeler found and bisected that while unifying the
vsprintf format decoding in:

  fef20d9: vsprintf: unify the format decoding layer for its 3 users

The sign flag has been dropped out in favour of
precise types (ie: LONG/ULONG).

But the format helper number() still needs this flag to keep track of
the signedness unless it will consider all numbers as unsigned.

Also add an explicit cast to int (for %d) while parsing with va_arg()
to ensure the highest bit is well extended on the 64 bits number that
hosts the value in case of negative values.

Reported-Bisected-Tested-by: Sitsofe Wheeler <sitsofe@yahoo.com>
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Lai Jiangshan <laijs@cn.fujitsu.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
LKML-Reference: <20090309201503.GA5010@nowhere>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
  • Loading branch information
Frederic Weisbecker authored and Ingo Molnar committed Mar 10, 2009
1 parent fef20d9 commit 39e874f
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/vsprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,6 @@ static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
static int format_decode(const char *fmt, struct printf_spec *spec)
{
const char *start = fmt;
bool sign = false;

/* we finished early by reading the field width */
if (spec->type == FORMAT_TYPE_WITDH) {
Expand Down Expand Up @@ -900,7 +899,7 @@ static int format_decode(const char *fmt, struct printf_spec *spec)

case 'd':
case 'i':
sign = true;
spec->flags |= SIGN;
case 'u':
break;

Expand All @@ -912,7 +911,7 @@ static int format_decode(const char *fmt, struct printf_spec *spec)
if (spec->qualifier == 'L')
spec->type = FORMAT_TYPE_LONG_LONG;
else if (spec->qualifier == 'l') {
if (sign)
if (spec->flags & SIGN)
spec->type = FORMAT_TYPE_LONG;
else
spec->type = FORMAT_TYPE_ULONG;
Expand All @@ -921,12 +920,12 @@ static int format_decode(const char *fmt, struct printf_spec *spec)
} else if (spec->qualifier == 't') {
spec->type = FORMAT_TYPE_PTRDIFF;
} else if (spec->qualifier == 'h') {
if (sign)
if (spec->flags & SIGN)
spec->type = FORMAT_TYPE_SHORT;
else
spec->type = FORMAT_TYPE_USHORT;
} else {
if (sign)
if (spec->flags & SIGN)
spec->type = FORMAT_TYPE_INT;
else
spec->type = FORMAT_TYPE_UINT;
Expand Down Expand Up @@ -1101,8 +1100,8 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
case FORMAT_TYPE_SHORT:
num = (short) va_arg(args, int);
break;
case FORMAT_TYPE_UINT:
num = va_arg(args, unsigned int);
case FORMAT_TYPE_INT:
num = (int) va_arg(args, int);
break;
default:
num = va_arg(args, unsigned int);
Expand Down

0 comments on commit 39e874f

Please sign in to comment.