public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Laszlo Ersek <lersek@redhat.com>
To: "Cohen, Eugene" <eugene@hp.com>
Cc: "edk2-devel@lists.01.org" <edk2-devel@ml01.01.org>,
	"Kinney, Michael D" <michael.d.kinney@intel.com>,
	Alexei Fedorov <Alexei.Fedorov@arm.com>
Subject: Re: What is the right way to print a UINTN?
Date: Mon, 26 Sep 2016 17:31:36 +0200	[thread overview]
Message-ID: <0de4dd03-faa7-1608-9625-369ab5d6e682@redhat.com> (raw)
In-Reply-To: <AT5PR84MB0291232000D2209AD7B53D52B4CD0@AT5PR84MB0291.NAMPRD84.PROD.OUTLOOK.COM>

On 09/26/16 15:46, Cohen, Eugene wrote:
> Get ready for a potentially stupid question (or at least a question I
> probably should know the answer to by now)...

This is a perfectly valid question.

I print INTN / UINTN values with:
- casting them unconditionally to INT64 / UINT64,
- printing the converted values with the matching conversion specifiers,
such as %Ld (for INT64) and %Lu or %Lx (for UINT64).

This solution requires a bit more typing, and it is a bit pessimistic
for 32-bit builds. On the positive side, it is robust / portable, and
completely valid C.

It is inspired by the standard C types intmax_t / uintmax_t. If you
write portable C code and want to print a value of some integer type,
where the spec only states "signed" or "unsigned integer type", but the
actual type is either implementation defined or unspecified, converting
the value to intmax_t / uintmax_t, and then printing it with %jd vs. %ju
/ %jx, is safe.

Regarding %p (relayed by Alexei as a recommendation from years ago),
that's a bug. %p takes a pointer to void. If we ever enable format
string checking in edk2, for example with GCC's
__attribute__((format(printf, ..., ...))), all those calls will blow up.

Thanks
Laszlo


  parent reply	other threads:[~2016-09-26 15:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-26 13:46 What is the right way to print a UINTN? Cohen, Eugene
2016-09-26 14:39 ` Alexei Fedorov
2016-09-26 15:31 ` Laszlo Ersek [this message]
2016-09-27 12:29   ` Cohen, Eugene
2016-09-27 14:30     ` Laszlo Ersek
2016-09-27 16:03       ` Cohen, Eugene
2016-09-27 16:31         ` Laszlo Ersek
2016-09-27 16:47         ` Andrew Fish
2016-09-27 17:14           ` Brian J. Johnson
2016-09-27 18:31             ` Laszlo Ersek
2016-09-27 20:27             ` Kinney, Michael D
2016-09-27 17:27           ` Kinney, Michael D
2016-09-27 17:46             ` Andrew Fish
2016-09-27 18:20               ` Kinney, Michael D
2016-09-27 19:28             ` Cohen, Eugene
2016-09-27 20:10               ` Kinney, Michael D

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-list 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=0de4dd03-faa7-1608-9625-369ab5d6e682@redhat.com \
    --to=devel@edk2.groups.io \
    /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