Re: [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'

From: Stephen Rothwell
Date: Sun May 06 2012 - 20:20:42 EST


Hi Pierre,

Thanks for doing this. Comments below.

On Sat, 5 May 2012 16:32:30 +0200 Pierre Carrier <pierre@xxxxxxxxxxx> wrote:
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index abbabec..7129383 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -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_null = num == 0LL;

"num" is a number not a pointer, so I would call it "is_zero" (or just do
the comparisons explicitly below).

> /* locase = 0 or 0x20. ORing digits or letters with 'locase'
> * produces same digits or (maybe lowercased) letters */
> @@ -353,9 +354,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_null) {
> + if (buf < end)
> + *buf = '0';
> + ++buf;
> + }
> if (spec.base == 16) {
> if (buf < end)
> *buf = ('X' | locase);

There is also another earlier section that tests "need_pfx" that reduces
the field width. You should fix that up as well.

--
Cheers,
Stephen Rothwell sfr@xxxxxxxxxxxxxxxx
http://www.canb.auug.org.au/~sfr/

Attachment: pgp00000.pgp
Description: PGP signature