public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* printf: Ambiguous warning
@ 2021-03-16 10:20 Rene Kita
  2021-03-16 10:26 ` Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Rene Kita @ 2021-03-16 10:20 UTC (permalink / raw)
  To: gcc; +Cc: mail

(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.

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


% gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/home/kt/bin/gcc-latest/libexec/gcc/x86_64-pc-linux-gnu/11.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --disable-multilib --disable-werror
--enable-languages=c,c++,go --disable-bootstrap --disable-nls
--enable-threads=posix --enable-default-pie --enable-checking=release
--program-suffix=-latest --prefix=/home/kt/bin/gcc-latest
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.0.1 20210315 (experimental) (GCC) 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: printf: Ambiguous warning
  2021-03-16 10:20 printf: Ambiguous warning Rene Kita
@ 2021-03-16 10:26 ` Jakub Jelinek
  2021-03-16 10:46   ` Rene Kita
  0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2021-03-16 10:26 UTC (permalink / raw)
  To: Rene Kita, gcc

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: printf: Ambiguous warning
  2021-03-16 10:26 ` Jakub Jelinek
@ 2021-03-16 10:46   ` Rene Kita
  2021-03-16 11:23     ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Rene Kita @ 2021-03-16 10:46 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc

On Tue, Mar 16, 2021 at 11:26:29AM +0100, Jakub Jelinek wrote:
> On Tue, Mar 16, 2021 at 11:20:05AM +0100, Rene Kita wrote:
> > % 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 *.
I understand this and I don't say the warning is wrong but the suggested
solution. I have highlighted the part of the output I'm talking about
above. If you replace the '%hn' with e.g. '%d' you get the same
suggestion:

main.c: In function 'main':
main.c:10:15: warning: format '%d' expects argument of type 'int', but argument 2 has type 'short unsigned int *' [-Wformat=]
   10 |   printf("p: %d\n", p);
      |              ~^     ~
      |               |     |
      |               int   short unsigned int *
      |              %hn
                    ^^^^^

> 	Jakub
Rene

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: printf: Ambiguous warning
  2021-03-16 10:46   ` Rene Kita
@ 2021-03-16 11:23     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2021-03-16 11:23 UTC (permalink / raw)
  To: mail; +Cc: Jakub Jelinek, gcc

On Tue, 16 Mar 2021 at 10:47, Rene Kita <mail@rkta.de> wrote:
>
> On Tue, Mar 16, 2021 at 11:26:29AM +0100, Jakub Jelinek wrote:
> > On Tue, Mar 16, 2021 at 11:20:05AM +0100, Rene Kita wrote:
> > > % 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 *.
> I understand this and I don't say the warning is wrong but the suggested
> solution. I have highlighted the part of the output I'm talking about
> above. If you replace the '%hn' with e.g. '%d' you get the same
> suggestion:
>
> main.c: In function 'main':
> main.c:10:15: warning: format '%d' expects argument of type 'int', but argument 2 has type 'short unsigned int *' [-Wformat=]
>    10 |   printf("p: %d\n", p);
>       |              ~^     ~
>       |               |     |
>       |               int   short unsigned int *
>       |              %hn
>                     ^^^^^

This looks similar to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98819

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-03-16 11:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-16 10:20 printf: Ambiguous warning Rene Kita
2021-03-16 10:26 ` Jakub Jelinek
2021-03-16 10:46   ` Rene Kita
2021-03-16 11:23     ` Jonathan Wakely

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).