public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Rene Kita <mail@rkta.de>, gcc@gcc.gnu.org
Subject: Re: printf: Ambiguous warning
Date: Tue, 16 Mar 2021 11:26:29 +0100	[thread overview]
Message-ID: <20210316102629.GU231854@tucnak> (raw)
In-Reply-To: <YFCGVU/zVQ3BJe9R@t480>

On Tue, Mar 16, 2021 at 11:20:05AM +0100, Rene Kita wrote:
> (Please keep me CC'd, I'm not subscribe to the list)
> 
> Here is a minimal example:
> #include <stdio.h>
> 
> int
> main()
> {
>   unsigned short n;
>   unsigned short *p;
>   p = &n;
> 
>   printf("p: %hn\n", p);
> }
> 
> 
> % gcc -Wall -Wpedantic main.c
> main.c: In function 'main':
> main.c:10:16: warning: format '%hn' expects argument of type 'short int *', but argument 2 has type 'short unsigned int *' [-Wformat=]
>    10 |   printf("p: %hn\n", p);
>       |              ~~^     ~
>       |                |     |
>       |                |     short unsigned int *
>       |                short int *
>       |              %hn
> 
> The warning for line 10 suggests to use '%hn' as format specifier which
> is already used and the wrong one. AFAIK the correct format specifier
> would be '%p' here.

No, the warning tells you that argument for %hn should have short int *
type, not unsigned short int *.
C99 says for n:
"The argument shall be a pointer to signed integer into which is written the
number of characters written to the output stream so far by this call to
fprintf. No argument is converted, but one is consumed. If the conversion
specification includes any flags, a field width, or a precision, the behavior is
undefined."
Note the SIGNED in there.  And for the h modifier:
"or that a following n conversion specifier applies to a pointer to a short
int argument."
(not unsigned short int).

> I didn't not find a bug report for this. Is this a known problem? Should
> I file a bug report for this?

No, this is a bug in your testcase that is correctly diagnosed.

	Jakub


  reply	other threads:[~2021-03-16 10:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-16 10:20 Rene Kita
2021-03-16 10:26 ` Jakub Jelinek [this message]
2021-03-16 10:46   ` Rene Kita
2021-03-16 11:23     ` Jonathan Wakely

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=20210316102629.GU231854@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=mail@rkta.de \
    /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).