[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: Eli Zaretskii
Subject: bug#32189: 27.0.50; GCC 7 warning due to -Wformat-truncation=2
Date: Thu, 19 Jul 2018 16:29:37 +0300

> From: Philipp Stephani <address@hidden>
> Date: Thu, 19 Jul 2018 08:21:54 +0200
> Cc: Ken Brown <address@hidden>, address@hidden
>  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.

That might be typical for your uses, but it doesn't mean it is
universally so.  It definitely isn't true for my uses: I use snprintf
to be sure a fixed buffer won't be overrun due to some programming
mistake.  And that is exactly what is going on in that function,
because the maximum expected values of the variables are known in
advance, and the buffer is large enough to accommodate them.

> 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.

Since truncation should never happen, except when there's some bug,
this is not really interesting.

> Rather than relying on truncation, the code should probably just use a buffer 
> that's large enough, or use %g
> instead of %f. 

If we need to jump through hoops to stop useless noise about perfectly
valid code, we shouldn't have this warning in the first place.

reply via email to

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