lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
authorPierre Carrier <pierre@spotify.com>
Tue, 29 May 2012 22:07:35 +0000 (15:07 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 29 May 2012 23:22:33 +0000 (16:22 -0700)
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 <pierre@spotify.com>
Acked-by: Stephen Rothwell <sfr@canb.auug.org.au>
Cc: Joe Perches <joe@perches.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
lib/vsprintf.c

index f5dfe0ca34f60c25a1367da581dd0c842db1ff87..5391299c1e7852dcc820a9e14ef88720fa949044 100644 (file)
@@ -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 */
@@ -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--;
        }
 
@@ -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);