public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Joe Perches' <joe@perches.com>, LKML <linux-kernel@vger.kernel.org>
Cc: kernel-janitors <kernel-janitors@vger.kernel.org>
Subject: RE: Use of '%#02x' is almost always wrong
Date: Tue, 12 Oct 2021 21:10:32 +0000	[thread overview]
Message-ID: <f41e216ed5074c55ad024e68a7dfaa3c@AcuMS.aculab.com> (raw)
In-Reply-To: <b04a10ea9682c51764053c1be73d4b3be59d1e7f.camel@perches.com>

From: Joe Perches
> Sent: 10 October 2021 17:46
> 
> Perhaps too many developers do not know that using '%#<width>x'
> in printf output _includes_ the 0x prefix in the output width.
> 
> So %#02x already has the width taken by the 0x prefix and will
> always emit 3 or more output bytes.
> 
> sprintf(buf, "%#02x", 1) emits 0x1 (3 bytes of output)
> sprintf(buf, "%#02x", 255) emits 0xff (4 bytes of output)
> 
> So presumably all the uses of %#02x in the kernel are not outputting
> what is actually expected.  Perhaps all of these should use %#04x.

Doesn't help. The definition of "%#x" is completely broken.
Basically 0 doesn't get the "0x" prefix, so "%#04x" outputs
"0000" if the value is zero.
So the correct replacement is (probably) "0x%02x".

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


  reply	other threads:[~2021-10-12 21:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-10 16:45 Use of '%#02x' is almost always wrong Joe Perches
2021-10-12 21:10 ` David Laight [this message]
2021-10-13 23:16   ` Bernd Petrovitsch
2021-10-14  8:27     ` David Laight
2021-10-13  6:15 ` Dan Carpenter
2021-10-13  8:17   ` Joe Perches
2021-10-13 10:39     ` Dan Carpenter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f41e216ed5074c55ad024e68a7dfaa3c@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=joe@perches.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox