public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "luigighiron at gmail dot com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug libc/31470] New: inttypes.h provides incorrect macro definitions
Date: Mon, 11 Mar 2024 01:05:29 +0000	[thread overview]
Message-ID: <bug-31470-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=31470

            Bug ID: 31470
           Summary: inttypes.h provides incorrect macro definitions
           Product: glibc
           Version: unspecified
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: luigighiron at gmail dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

The inttypes.h header provides macros for printing out the types defined in
inttypes.h and stdint.h. In C23, the following text was added to the C standard
in section 7.8.1 "Macros for format specifiers":

> The functions in the fprintf and fwprintf families shall behave as if they use
> va_arg with a type argument naming the type resulting from applying the
> default argument promotions to the type corresponding to the macro and then
> convert the result of the va_arg expansion to the type corresponding to the
> macro.

For example:

#include<stdio.h>
#include<inttypes.h>
int main(){
    uint8_t u=-1;
    printf("%"PRIu8"\n",u+1);
}

This program should output 0, at least in C23. u contains the value 255, then
one is added which results in the value 256 (after integer promotions). 256 is
provided to printf which will do va_arg with int (the promoted type of uint8_t)
and that value is then converted back to uint8_t which will result in the value
0.

However, libc (I tested it on godbolt) seems to print 256 here because it does
not do the conversion back to uint8_t. This seems to be because libc defines
PRIu8 here as "u". I tested this on MSVC and Cygwin and it printed 0 as
expected, with those compilers it is defined as "hhu". The text which requires
that the conversion back after va_arg was added by GB-108 (N3148). See N3167
for discussion about accepting GB-108.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2024-03-11  1:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-11  1:05 luigighiron at gmail dot com [this message]
2024-03-11 12:43 ` [Bug libc/31470] " fweimer at redhat dot com
2024-03-11 13:12 ` schwab@linux-m68k.org
2024-03-11 14:44 ` luigighiron at gmail dot com
2024-03-11 15:25 ` schwab@linux-m68k.org
2024-03-11 15:34 ` jsm28 at gcc dot gnu.org
2024-03-11 15:36 ` jsm28 at gcc dot gnu.org

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=bug-31470-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).