[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#32189: 27.0.50; GCC 7 warning due to -Wformat-truncation=2

From: Philipp Stephani
Subject: bug#32189: 27.0.50; GCC 7 warning due to -Wformat-truncation=2
Date: Thu, 19 Jul 2018 08:21:54 +0200

Eli Zaretskii <address@hidden> schrieb am Mi., 18. Juli 2018 um 17:10 Uhr:
> From: Ken Brown <address@hidden>
> Date: Tue, 17 Jul 2018 15:26:34 -0400
> I'm getting the following warning when building the master branch on
> Cygwin with GCC 7.3:
> In file included from /usr/include/stdio.h:800:0,
>                   from ../lib/stdio.h:43,
>                   from ../../master/src/w32cygwinx.c:22:
> ../../master/src/w32cygwinx.c: In function ‘Fw32_battery_status’:
> ../../master/src/w32cygwinx.c:116:26: warning: ‘%3.1f’ directive output
> may be truncated writing between 3 and 312 bytes into a region of size
> 16 [-Wformat-truncation=]
>      snprintf (buffer, 16, "%3.1f", h);
>                            ^
> ../../master/src/w32cygwinx.c:116:4: note: ‘__builtin_snprintf’ output
> between 4 and 313 bytes into a destination of size 16
>      snprintf (buffer, 16, "%3.1f", h);
>      ^

Do we really need to use -Wformat-truncation?  Is it a useful warning
switch?  The above sounds like useless noise, because the code
explicitly _asks_ for truncation.  What do people think about this?

The typical use case for snprintf is to make sure that the output never gets truncated, so disabling -Wformat-truncation would be harmful in most cases.
With truncation, the code will result in nonsensical output if the input value was too large, e.g. 123.4 hours would result in "12" if the buffer size were 3.
Rather than relying on truncation, the code should probably just use a buffer that's large enough, or use %g instead of %f. 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]