public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* help on inconsistent printf() warning from gcc
@ 2014-10-31 18:49 Joel Sherrill
  2014-10-31 19:07 ` Joseph S. Myers
  2014-10-31 19:13 ` Craig Howland
  0 siblings, 2 replies; 7+ messages in thread
From: Joel Sherrill @ 2014-10-31 18:49 UTC (permalink / raw)
  To: newlib

[-- Attachment #1: Type: text/plain, Size: 1211 bytes --]

Hi

In tracking down warnings in RTEMS, we have one which
is for a printf() format specifier on wchar_t. The code
originated in FreeBSD and the initial code we had did
have the wrong specifier. But this was flagged on a subset
of targets. When I corrected the warning, the situation
changed and some targets gave false positive.

I have attached correct and incorrect test cases. I am hoping
someone here can give me a clue as to where to look. I am
wondering if this is an inconsistency across targets in the
C library. I did file this as GCC PR63301 but it was quickly
closed as invalid. But I think the inconsistent generation of
warnings is a bug.

The test script "j" and its output are attached. Notice some
targets have no warnings, some warning on both, and mixes.
I would expect the output to be similar on *-elf targets.

Any suggestions on how to address this? I would love to
see our code be warning free on all targets.

Thanks.

-- 
Joel Sherrill, Ph.D.             Director of Research & Development
joel.sherrill@OARcorp.com        On-Line Applications Research
Ask me about RTEMS: a free RTOS  Huntsville AL 35805
Support Available                (256) 722-9985


[-- Attachment #2: correct_wchar.c --]
[-- Type: text/plain, Size: 221 bytes --]

/*
 * This code uses the correct printf() format specifier and should
 * not produce a warning.
 */
#include <wchar.h>
#include <stdio.h>
int main(void)
{
  wchar_t wc= L'a';
  printf("%C", wc);
  return 0;
}

[-- Attachment #3: incorrect_wchar.c --]
[-- Type: text/plain, Size: 220 bytes --]

/*
 * This code uses the incorrect printf() format specifier and should
 * produce a warning.
 */
#include <wchar.h>
#include <stdio.h>
int main(void)
{
  wchar_t wc= L'a';
  printf("%lc", wc);
  return 0;
}

[-- Attachment #4: j --]
[-- Type: text/plain, Size: 412 bytes --]

for t in arm-rtems4.11 avr-rtems4.11 bfin-rtems4.11 h8300-rtems4.11 \
  i386-rtems4.11 lm32-rtems4.11 m32c-rtems4.11 m32r-rtems4.11 \
  m68k-rtems4.11 mips-rtems4.11 moxie-rtems4.11 nios2-rtems4.11 \
  powerpc-rtems4.11 sh-rtems4.11 sparc64-rtems4.11 sparc-rtems4.11 \
  v850-rtems4.11
do
  echo === ${t}
  ${t}-gcc -Wall -Wextra -c correct_wchar.c
  ${t}-gcc -Wall -Wextra -c incorrect_wchar.c
done


[-- Attachment #5: output.txt --]
[-- Type: text/plain, Size: 4192 bytes --]

=== arm-rtems4.11
=== avr-rtems4.11
=== bfin-rtems4.11
=== h8300-rtems4.11
=== i386-rtems4.11
=== lm32-rtems4.11
=== m32c-rtems4.11
correct_wchar.c: In function 'main':
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%C", wc);
   ^
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
incorrect_wchar.c: In function 'main':
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%lc", wc);
   ^
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
=== m32r-rtems4.11
=== m68k-rtems4.11
correct_wchar.c: In function 'main':
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%C", wc);
   ^
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
incorrect_wchar.c: In function 'main':
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%lc", wc);
   ^
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
=== mips-rtems4.11
=== moxie-rtems4.11
=== nios2-rtems4.11
=== powerpc-rtems4.11
correct_wchar.c: In function 'main':
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%C", wc);
   ^
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
incorrect_wchar.c: In function 'main':
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%lc", wc);
   ^
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
=== sh-rtems4.11
correct_wchar.c: In function 'main':
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%C", wc);
   ^
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
incorrect_wchar.c: In function 'main':
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%lc", wc);
   ^
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
=== sparc64-rtems4.11
=== sparc-rtems4.11
correct_wchar.c: In function 'main':
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%C", wc);
   ^
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
incorrect_wchar.c: In function 'main':
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%lc", wc);
   ^
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
=== v850-rtems4.11
correct_wchar.c: In function 'main':
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%C", wc);
   ^
correct_wchar.c:10:3: warning: format '%C' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
incorrect_wchar.c: In function 'main':
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]
   printf("%lc", wc);
   ^
incorrect_wchar.c:10:3: warning: format '%lc' expects argument of type 'wint_t', but argument 2 has type 'wchar_t' [-Wformat=]

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

* Re: help on inconsistent printf() warning from gcc
  2014-10-31 18:49 help on inconsistent printf() warning from gcc Joel Sherrill
@ 2014-10-31 19:07 ` Joseph S. Myers
  2014-10-31 19:13 ` Craig Howland
  1 sibling, 0 replies; 7+ messages in thread
From: Joseph S. Myers @ 2014-10-31 19:07 UTC (permalink / raw)
  To: Joel Sherrill; +Cc: newlib

On Fri, 31 Oct 2014, Joel Sherrill wrote:

> Hi
> 
> In tracking down warnings in RTEMS, we have one which
> is for a printf() format specifier on wchar_t. The code

The formats in question take wint_t, not wchar_t.

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: help on inconsistent printf() warning from gcc
  2014-10-31 18:49 help on inconsistent printf() warning from gcc Joel Sherrill
  2014-10-31 19:07 ` Joseph S. Myers
@ 2014-10-31 19:13 ` Craig Howland
  2014-10-31 20:04   ` Joel Sherrill
                     ` (2 more replies)
  1 sibling, 3 replies; 7+ messages in thread
From: Craig Howland @ 2014-10-31 19:13 UTC (permalink / raw)
  To: newlib


On 10/31/2014 02:49 PM, Joel Sherrill wrote:
> Hi
>
> ...
>
> I have attached correct and incorrect test cases. I am hoping
> someone here can give me a clue as to where to look. I am
> wondering if this is an inconsistency across targets in the
> C library. I did file this as GCC PR63301 but it was quickly
> closed as invalid. But I think the inconsistent generation of
> warnings is a bug.
Not that this changes the behavior, but the test cases are labeled backwards.  
After applying the wchar_t-to win_t correction that Myers points out, the 
correct format is %lc (per C99), while %C is non-standard.

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

* Re: help on inconsistent printf() warning from gcc
  2014-10-31 19:13 ` Craig Howland
@ 2014-10-31 20:04   ` Joel Sherrill
  2014-10-31 20:21     ` Joel Sherrill
  2014-10-31 20:54   ` Corinna Vinschen
  2014-10-31 22:25   ` Eric Blake
  2 siblings, 1 reply; 7+ messages in thread
From: Joel Sherrill @ 2014-10-31 20:04 UTC (permalink / raw)
  To: Craig Howland, newlib


On 10/31/2014 2:13 PM, Craig Howland wrote:
> On 10/31/2014 02:49 PM, Joel Sherrill wrote:
>> Hi
>>
>> ...
>>
>> I have attached correct and incorrect test cases. I am hoping
>> someone here can give me a clue as to where to look. I am
>> wondering if this is an inconsistency across targets in the
>> C library. I did file this as GCC PR63301 but it was quickly
>> closed as invalid. But I think the inconsistent generation of
>> warnings is a bug.
> Not that this changes the behavior, but the test cases are labeled backwards.  
> After applying the wchar_t-to win_t correction that Myers points out, the 
> correct format is %lc (per C99), while %C is non-standard.
>
Thanks. That is much more helpful. Hopefully I can resolve this
Monday in the code and eliminate another handful of warnings.

I rather feel stupid. :(

-- 
Joel Sherrill, Ph.D.             Director of Research & Development
joel.sherrill@OARcorp.com        On-Line Applications Research
Ask me about RTEMS: a free RTOS  Huntsville AL 35805
Support Available                (256) 722-9985

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

* Re: help on inconsistent printf() warning from gcc
  2014-10-31 20:04   ` Joel Sherrill
@ 2014-10-31 20:21     ` Joel Sherrill
  0 siblings, 0 replies; 7+ messages in thread
From: Joel Sherrill @ 2014-10-31 20:21 UTC (permalink / raw)
  To: Craig Howland, newlib


On 10/31/2014 3:04 PM, Joel Sherrill wrote:
> On 10/31/2014 2:13 PM, Craig Howland wrote:
>> On 10/31/2014 02:49 PM, Joel Sherrill wrote:
>>> Hi
>>>
>>> ...
>>>
>>> I have attached correct and incorrect test cases. I am hoping
>>> someone here can give me a clue as to where to look. I am
>>> wondering if this is an inconsistency across targets in the
>>> C library. I did file this as GCC PR63301 but it was quickly
>>> closed as invalid. But I think the inconsistent generation of
>>> warnings is a bug.
>> Not that this changes the behavior, but the test cases are labeled backwards.  
>> After applying the wchar_t-to win_t correction that Myers points out, the 
>> correct format is %lc (per C99), while %C is non-standard.
>>
> Thanks. That is much more helpful. Hopefully I can resolve this
> Monday in the code and eliminate another handful of warnings.
>
> I rather feel stupid. :(
Hmm... I am confused now. Our source is based on FreeBSD hexdump.
This is from conv.c. The printf() is line 137. wc is still a wchar_t in the
current version.  They use %C so it must be a BSD-ism. 

http://svnweb.freebsd.org/base/head/usr.bin/hexdump/conv.c?revision=247753&view=markup

Any suggestions looking at the original source on how to fix this?
Is adding a cast to wint_t in the printf() at line 137 enough.

I just don't ever do wide character programming. :(

-- 
Joel Sherrill, Ph.D.             Director of Research & Development
joel.sherrill@OARcorp.com        On-Line Applications Research
Ask me about RTEMS: a free RTOS  Huntsville AL 35805
Support Available                (256) 722-9985

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

* Re: help on inconsistent printf() warning from gcc
  2014-10-31 19:13 ` Craig Howland
  2014-10-31 20:04   ` Joel Sherrill
@ 2014-10-31 20:54   ` Corinna Vinschen
  2014-10-31 22:25   ` Eric Blake
  2 siblings, 0 replies; 7+ messages in thread
From: Corinna Vinschen @ 2014-10-31 20:54 UTC (permalink / raw)
  To: newlib

[-- Attachment #1: Type: text/plain, Size: 1542 bytes --]

On Oct 31 15:13, Craig Howland wrote:
> 
> On 10/31/2014 02:49 PM, Joel Sherrill wrote:
> >Hi
> >
> >...
> >
> >I have attached correct and incorrect test cases. I am hoping
> >someone here can give me a clue as to where to look. I am
> >wondering if this is an inconsistency across targets in the
> >C library. I did file this as GCC PR63301 but it was quickly
> >closed as invalid. But I think the inconsistent generation of
> >warnings is a bug.
> Not that this changes the behavior, but the test cases are labeled
> backwards.  After applying the wchar_t-to win_t correction that Myers points
> out, the correct format is %lc (per C99), while %C is non-standard.

%C is not exactly non-standard.  %C is allowed and documented as an
optional XSI extension in SUSv4, being equivalent to %lc:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html

But still, %lc and %C both expect an wint_t argument.  Quote from
the above SUSV4 man page:

  c
    The int argument shall be converted to an unsigned char, and the
    resulting byte shall be written.

    If an l (ell) qualifier is present, the wint_t argument shall be
    converted as if by an ls conversion specification with no precision
    and an argument that points to a two-element array of type wchar_t,
    the first element of which contains the wint_t argument to the ls
    conversion specification and the second element contains a null wide
    character.


Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: help on inconsistent printf() warning from gcc
  2014-10-31 19:13 ` Craig Howland
  2014-10-31 20:04   ` Joel Sherrill
  2014-10-31 20:54   ` Corinna Vinschen
@ 2014-10-31 22:25   ` Eric Blake
  2 siblings, 0 replies; 7+ messages in thread
From: Eric Blake @ 2014-10-31 22:25 UTC (permalink / raw)
  To: Craig Howland, newlib

[-- Attachment #1: Type: text/plain, Size: 1551 bytes --]

On 10/31/2014 01:13 PM, Craig Howland wrote:
> 
> On 10/31/2014 02:49 PM, Joel Sherrill wrote:
>> Hi
>>
>> ...
>>
>> I have attached correct and incorrect test cases. I am hoping
>> someone here can give me a clue as to where to look. I am
>> wondering if this is an inconsistency across targets in the
>> C library. I did file this as GCC PR63301 but it was quickly
>> closed as invalid. But I think the inconsistent generation of
>> warnings is a bug.
> Not that this changes the behavior, but the test cases are labeled
> backwards.  After applying the wchar_t-to win_t correction that Myers
> points out, the correct format is %lc (per C99), while %C is non-standard.

Wrong.  Per POSIX, %C is identical to C99's %lc:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html

However, your examples are also doing it wrong.  Re-read the %lc
description:

%c
    If an l (ell) qualifier is present, the wint_t argument shall be
converted as if by an ls conversion specification with no precision and
an argument that points to a two-element array of type wchar_t, the
first element of which contains the wint_t argument to the ls conversion
specification and the second element contains a null wide character.

You weren't passing a NUL-terminated wint_t array:

  wchar_t wc= L'a';
  printf("%C", wc);

Correct usage would look more like:

  wint_t wc[2] = L"a";
  printf("%C", wc);

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

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

end of thread, other threads:[~2014-10-31 22:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-31 18:49 help on inconsistent printf() warning from gcc Joel Sherrill
2014-10-31 19:07 ` Joseph S. Myers
2014-10-31 19:13 ` Craig Howland
2014-10-31 20:04   ` Joel Sherrill
2014-10-31 20:21     ` Joel Sherrill
2014-10-31 20:54   ` Corinna Vinschen
2014-10-31 22:25   ` Eric Blake

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