Ticket #39224

GCC-7.3 and later apply wrong -Wformat attributes for snprintf() and vsnprintf() functions

Open Date: 2019-05-06 23:51 Last Update: 2019-07-05 19:18

Reporter:
Owner:
Type:
Status:
Closed
Component:
MileStone:
(None)
Priority:
5 - Medium
Severity:
5 - Medium
Resolution:
Fixed
File:
2
Vote
Score: 0
No votes
0.0% (0/0)
0.0% (0/0)

Details

As originally reported by Eli Zaretskii, in this mingw-users mail posting, use of ISO-C99 standard format specifications, such as "%02hhx", (which is not understood by Microsoft's MSVCRT.DLL family of printf() functions), in calls to snprintf() may result in unexpected compile-time warning diagnostics, when compiled with any warning option, such as -Wall, which implicitly (or even explicitly) enables -Wformat.

The issue appears to be caused by GCC's use of an internal prototype, for snprintf(), which seems to arbitrarily assume Microsoft's -Wformat semantics, (even though Microsoft have never provided a snprintf() implementation, in any version of the system-supplied MSVCRT.DLL runtime library, on which every MinGW-GCC compiler build is dependent). The issue does not arise when calls to snprintf() are replaced by its equivalent __mingw_snprintf(), (which is explicitly declared as exhibiting MinGW's -Wformat semantics); nor does it arise when GCC's internal prototype is overridden, (by use of the -fno-builtin-snprintf option), and thus the <stdio.h> prototype, (which also explicitly exhibits MinGW's -Wformat semantics), becomes applicable.

It is anticipated that the vsnprintf() implementation will be similarly affected.

Ticket History (3/7 Histories)

2019-05-06 23:51 Updated by: keith
  • New Ticket "GCC-7.3 and later apply wrong -Wformat attributes for snprintf() and vsnprintf() functions" created
2019-05-06 23:59 Updated by: keith
  • File snprintf-stdio-declarations.patch (File ID: 5465) is attached
2019-05-06 23:59 Updated by: keith
  • File snprintf-stdio-declarations.patch (File ID: 5465) is deleted
2019-07-04 03:18 Updated by: keith
Comment

There is, in fact, a defect in the MinGW -Wformat handler; it affects the handling of the "%n" conversion specification, when specified with the "hh" length modifier, (i.e. as "%hhn"). Although this combination is fully supported by the underlying pformat.c implementation, the associated -Wformat handler does not recognize this, and consequently emits an entirely bogus, and unhelpful warning, when this is parsed.

Fixing this isn't difficult, but it does require a (tedious) rebuild of GCC, to deploy it.

2019-07-05 19:18 Updated by: keith
  • Status Update from Open to Closed
  • Resolution Update from None to Fixed
Comment

The -Wformat mishandling of "%hhn" is corrected from release 4-mingw32 of GCC-8.2.0 onwards; at present, I do not plan to back-port to GCC-7.3.0 .

The inappropriate effect of interaction with GCC's built-in snprintf() and vsnprintf() prototypes is corrected by commit #21718ad; this will be included in the next mingwrt/w32api release.

(Edited, 2019-11-13 23:38 Updated by: keith)

Attachment File List

Edit

Please login to add comment to this ticket » Login