This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] avoid assuming strncpy arrays are nul-terminated (PR 89664)


On 3/19/19 12:01 PM, Martin Sebor wrote:
On 3/19/19 8:33 AM, Jeff Law wrote:
On 3/11/19 8:27 PM, Martin Sebor wrote:
The -Wstringop-truncation handling for strncpy/stpncpy neglects
to consider that character arrays tracked by the strlen pass
are not necessarily nul-terminated.  It unconditionally adds
one when computing the size of each sequence to account for
the nul.  This leads to false positive warnings when checking
the validity of indices/pointers computed by the built-ins.

The attached patch corrects this by adding one for the nul
only when the character array is known to be nul-terminated.

Since GCC 7 does not issue the warning this is a 8/9 regression
that I would like to fix in both releases.  Is the patch okay
for trunk/gcc-8-branch?

Tested on x86_64-linux.

Martin

gcc-89644.diff

PR tree-optimization/89644 - False-positive -Warray-bounds diagnostic on strncpy

gcc/ChangeLog:

    PR tree-optimization/89644
    * tree-ssa-strlen.c (handle_builtin_stxncpy): Consider unterminated
    arrays in determining sequence sizes in strncpy and stpncpy.

gcc/testsuite/ChangeLog:

    PR tree-optimization/89644
    * gcc.dg/Wstringop-truncation-8.c: New test.
OK for the trunk as well as the affected release branches.

I just noticed some some -Wrestrict test failures that I had missed
in the test report.  The patch wasn't quite right.  Sorry about that.
I'll fix it shortly.

The variable to use to check for nul-termination is full_string_p
rather than endptr.  I've fixed that in 269809.

Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]