Re: snprintf and %hhx format

Keith Marshall <[email protected]>
Newsgroups gmane.comp.gnu.mingw.user
Organization MinGW.org Project
Message-ID <[email protected]>
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

_______________________________________________
MinGW-Users mailing list
[email protected]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.osdn.me/mailman/listinfo/mingw-users
Also: mailto:[email protected]?subject=unsubscribe
signature.asc (application/pgp-signature, 836 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (GNU/Linux)

iQIcBAEBAgAGBQJcz2OpAAoJEMCtNsY0flo/S9EP/1QWUMVUTtWuIB60WCkGFcCA
1PiWnD9TAVdWlshw4k+Qgj8wwDs9c7IvfLvSvrjPjR7TaE6I3m1VamLAOuajTVGm
wFDBe1E/c9J49hn1mooKDfLO8a1EtyTtn+mhOU4Kn/UiNwTj62j3i458IwOMgbNV
8RkwYbn8rawuxZ/njZ0F+hrDt7gFLNpTVFP5iR+RI1LCBEjVVJYmiSkFfWU3I4nR
SxXY9/YPYUzL9y1qc0HGhj7NFJVA3jEqhOU/BMkG2bjv30yfF4G6bdF32s2fiIvG
VuXmKE5DuLlOHXmnqAbfzoWgcIbvmbu3xnS3yiKDxfhRk7SfZB6KK0Y2H1OyETAK
Dvc6L25hYftNHIUiO8UlWwHAQ0y8RdK7vsKReBEAyIApdWvuKqH9yTYVYh0sKv8h
Yl/wVV/1JWulzdY2+AZSJASX18J+MB7x9uzXVrulpoG1xvcG04coMlK2WqPBa6BX
ub7SFigmIckJVVCszL81CYdiY7Fm0EnogHTd+6+D1dL6K8/8Ldueme57DL81OWNK
JHHodUpVEZzd6HLxsYWUT9/7fSWTE7+V5m7lcppj5KBMfcZuTlS9FXAuWiuqMYXr
iBtubjiARRd+YQRgbnVtmIKSjGZPCUxPDQ3pt3Ate0X9mt7VIw8zajzJOPwlsCSy
bwG3DCemZs6F75uDXDpJ
=IREA
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.