https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104012
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Resolution|--- |INVALID Blocks| |85741 Status|UNCONFIRMED |RESOLVED --- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> --- I can reproduce the warning now with the command line options provided in comment #4: -O0 and -Wformat-truncation=2 are needed to trigger it. The interaction of the warning with optimizations (or their absence) is documented in the manual: When the exact number of bytes written by a format directive cannot be determined at compile-time it is estimated based on heuristics that depend on the level argument and on optimization. While enabling optimization will in most cases improve the accuracy of the warning, it may also result in false positives. -Wformat-truncation=2 Level 2 warns also about calls to bounded functions whose return value is used and that might result in truncation given an argument of sufficient length or magnitude. Not enabling optimization and using -Wformat-truncation=2 increases the chance that the heuristic of assuming the argument with greatest magnitude (for %04d it would be INT_MIN) will be used. GCC still does some value and range propagation even at -O0 but it doesn't inline function calls (like those to std::string members) or do other optimizations that might otherwise help it track data flow more accurately. Regardless of optimization, the intent of level 2 is to guide the programmer to provide a buffer that's big enough for the largest possible output (i.e., you have to either prove to GCC that the truncation isn't possible or handle it). At level 1 it's more like the other way around (GCC has to prove that the call will result in truncation in order to warn, although there's some fuzziness here and some heuristics apply as well). In short, this is not a bug. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85741 [Bug 85741] [meta-bug] bogus/missing -Wformat-overflow