[Mingw-users] snprintf and %hhx format

Back to archive index
Keith Marshall keith****@users*****
Mon May 6 07:28:57 JST 2019


On 05/05/19 15:23, Eli Zaretskii wrote:
> The following toy program:
> 
> #include <inttypes.h>
> #include <stdio.h>
> 
> int main (void)
> {
>   unsigned char c = 0x12;
>   char buf[3];
>   snprintf (buf, 3, "%02" SCNx8, c);
>   puts (buf);
> 
>   return 0;
> }
> 
> generates the following diagnostics:
> 
>   d:\usr\eli>gcc -Wall -o pt.exe pt.c
>   pt.c: In function 'main':
> pt.c:8:21: warning: unknown conversion type character 'h' in format
> [-Wformat=]
>      snprintf (buf, 3, "%02" SCNx8, c);
>                      ^~~~~
>   In file included from pt.c:1:
> d:\usr\include\inttypes.h:246:17: note: format string is defined
> here
>     #define SCNx8 "hhx"
>                     ^
> pt.c:8:21: warning: too many arguments for format
> [-Wformat-extra-args]
>      snprintf (buf, 3, "%02" SCNx8, c);
>                        ^~~~~
> 
> The warning goes away if I replace snprintf with __mingw_snprintf.

So, apparently the -Wformat attributes are correctly interpreted, when
explicitly delegating snprintf() calls to __mingw_snprintf()

> It also goes away if I add the -fno-builtin-snprintf compiler
> switch.

And also when we explicitly disable GCC's built-in snprintf().  I think
this is significant, because GCC's documentation states that it will
delegate to the built-in by default, and *that* appears to have the
wrong -Wformat attributes applied.

> So it looks like the __mingw_printf__ attribute fails to cause the
> compiler to recognize that our snprintf does support %hh?

No, I think it is working properly, when applied; it seems more likely
that GCC's built-in is not applying it correctly -- maybe because the
__mingw_printf__ -Wformat coding results from a local patch, and doesn't
originate upstream.  It was tricky enough to implement it at all; I
don't know how to get it applied to the built-in, without further
(possibly extensive) research.

> This is with GCC 8.2.0 and MinGW runtime 5.2.1.  I see the same
> warning with GCC 7.3.0.

It's the same local patch, in both cases.

> Am I doing something wrong?

One might question why you are using a scanf() format specifier, in a
printf() family call, but the macro expands to "%02hhx", which should
work for printf() calls too.  FWIW, I can reproduce your results, but I
don't see the same effect, if I substitute a (properly adjusted) call to
printf(), in place of snprintf().  We may need to tweak <stdio.h>, to
use similar call redirection for snprintf(), as is currently used for
printf(), fprintf(), and sprintf().

-- 
Regards,
Keith.

Public key available from keys.gnupg.net
Key fingerprint: C19E C018 1547 DE50 E1D4 8F53 C0AD 36C6 347E 5A3F

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <https://lists.osdn.me/mailman/archives/mingw-users/attachments/20190505/87903d51/attachment.sig>


More information about the MinGW-Users mailing list
Back to archive index