On 01/03/2017 05:04 PM, Martin Sebor wrote:
The -Wformat-length option warns about both overflow and truncation.
I had initially debated introducing two options, one for each of
the two kinds of problems, but decided to go with just one and
consider breaking it up based on feedback.
I feel that there has now been sufficient feedback (e.g., bugs 77708
and 78913) to justify breaking up the checkers and providing a new
option to control truncation independently.
The attached patch adds a new option, -Wformat-truncation=level that
accomplishes this. At level 1 the new option only warns on certain
truncation, or on likely truncation in snprintf calls whose return
value is unused (using the return value suppresses the warning in
these cases).
This change eliminates the -Wformat-length warnings from a build
of the Linux kernel, replacing their 43 instances with 32 of the
-Wformat-truncation warning. With one exception, they're all for
snprintf calls whose return values is unused (and thus possible
sources of bugs).
If/when this patch is approved I'd like to rename -Wformat-length
to -Wformat-overflow to make the option's refined purpose clear,
and for consistency with the -Wstringop-overflow option.
Martin
gcc-78913.diff
PR tree-optimization/78913 - Probably misleading error reported by
-Wformat-length
PR middle-end/77708 - -Wformat-length %s warns for snprintf
gcc/c-family/ChangeLog:
PR tree-optimization/78913
PR middle-end/77708
* c.opt (-Wformat-truncation): New option.
gcc/testsuite/ChangeLog:
PR tree-optimization/78913
PR middle-end/77708
* gcc.dg/tree-ssa/builtin-snprintf-warn-1.c: New test.
* gcc.dg/tree-ssa/builtin-snprintf-warn-2.c: New test.
* gcc.dg/tree-ssa/builtin-sprintf-warn-6.c: XFAIL test cases failing
due to bug 78969.
gcc/ChangeLog:
PR tree-optimization/78913
PR middle-end/77708
* doc/invoke.texi (Warning Options): Document -Wformat-truncation.
* gimple-ssa-sprintf.c (call_info::reval_used, call_info::warnopt):
New member functions.
(format_directive): Used them.
(add_bytes): Same.
(pass_sprintf_length::handle_gimple_call): Same.
OK.
jeff