Skip to content

Commit

Permalink
lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
Browse files Browse the repository at this point in the history
number()'s behaviour is slighly changed: 0 becomes "0" instead of "00"
when using the flag SPECIAL and base 8.

Before:
Number\Format  %o    %#o  %x    %#x
            0     0   00    0   0x0
            1     1   01    1   0x1
           16    20  020   10  0x10

After:
Number\Format  %o    %#o  %x    %#x
            0     0    0    0   0x0
            1     1   01    1   0x1
           16    20  020   10  0x10

Signed-off-by: Pierre Carrier <[email protected]>
Acked-by: Stephen Rothwell <[email protected]>
Cc: Joe Perches <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Pierre Carrier authored and torvalds committed May 29, 2012
1 parent 5536805 commit 7c20342
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/vsprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ char *number(char *buf, char *end, unsigned long long num,
char locase;
int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
int i;
bool is_zero = num == 0LL;

/* locase = 0 or 0x20. ORing digits or letters with 'locase'
* produces same digits or (maybe lowercased) letters */
Expand All @@ -305,8 +306,9 @@ char *number(char *buf, char *end, unsigned long long num,
}
}
if (need_pfx) {
spec.field_width--;
if (spec.base == 16)
spec.field_width -= 2;
else if (!is_zero)
spec.field_width--;
}

Expand Down Expand Up @@ -353,9 +355,11 @@ char *number(char *buf, char *end, unsigned long long num,
}
/* "0x" / "0" prefix */
if (need_pfx) {
if (buf < end)
*buf = '0';
++buf;
if (spec.base == 16 || !is_zero) {
if (buf < end)
*buf = '0';
++buf;
}
if (spec.base == 16) {
if (buf < end)
*buf = ('X' | locase);
Expand Down

0 comments on commit 7c20342

Please sign in to comment.