diff options
author | Martin Sebor <msebor@redhat.com> | 2017-12-06 17:47:45 +0000 |
---|---|---|
committer | Martin Sebor <msebor@gcc.gnu.org> | 2017-12-06 10:47:45 -0700 |
commit | 25b15e953ac48e5042e27a4419ca20211403b7cb (patch) | |
tree | 8702db003aa73c7b1a8670612c78ec7cd47578ea /gcc | |
parent | 4c413747a31d0dd8ecfc05312584725d2f0955f6 (diff) | |
download | gcc-25b15e953ac48e5042e27a4419ca20211403b7cb.zip gcc-25b15e953ac48e5042e27a4419ca20211403b7cb.tar.gz gcc-25b15e953ac48e5042e27a4419ca20211403b7cb.tar.bz2 |
PR tree-optimization/83075 - Invalid strncpy optimization
gcc/ChangeLog:
PR tree-optimization/83075
* tree-ssa-strlen.c (handle_builtin_stxncpy): Avoid assuming
strncat/strncpy don't change length of source string.
gcc/testsuite/ChangeLog:
PR tree-optimization/83075
* gcc.dg/tree-ssa/strncat.c: New test.
* gcc.dg/tree-ssa/strncpy-2.c: Same.
From-SVN: r255446
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/strncat.c | 19 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/strncpy-2.c | 19 | ||||
-rw-r--r-- | gcc/tree-ssa-strlen.c | 7 |
5 files changed, 53 insertions, 4 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ce17016..5bcc6a9 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2017-12-06 Martin Sebor <msebor@redhat.com> + + PR tree-optimization/83075 + * tree-ssa-strlen.c (handle_builtin_stxncpy): Avoid assuming + strncat/strncpy don't change length of source string. + 2017-12-06 Eric Botcazou <ebotcazou@adacore.com> Revert diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 9fef2d5..c39f3d2 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2017-12-06 Martin Sebor <msebor@redhat.com> + + PR tree-optimization/83075 + * gcc.dg/tree-ssa/strncat.c: New test. + * gcc.dg/tree-ssa/strncpy-2.c: Same. + 2017-12-06 Bin Cheng <bin.cheng@arm.com> * g++.dg/graphite/pr41305.C: Refine test option. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/strncat.c b/gcc/testsuite/gcc.dg/tree-ssa/strncat.c new file mode 100644 index 0000000..93a60c8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/strncat.c @@ -0,0 +1,19 @@ +/* PR tree-optimization/83075 - Invalid strncpy optimization + { dg-do run } + { dg-options "-O2 -Wno-stringop-overflow" } */ + +int main (void) +{ + char a[8] = ""; + + __builtin_strcpy (a, "123"); + + unsigned n0 = __builtin_strlen (a); + + __builtin_strncat (a + 3, a, n0); + + unsigned n1 = __builtin_strlen (a); + + if (n1 == n0) + __builtin_abort (); +} diff --git a/gcc/testsuite/gcc.dg/tree-ssa/strncpy-2.c b/gcc/testsuite/gcc.dg/tree-ssa/strncpy-2.c new file mode 100644 index 0000000..42203d5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/strncpy-2.c @@ -0,0 +1,19 @@ +/* PR tree-optimization/83075 - Invalid strncpy optimization + { dg-do run } + { dg-options "-O2 -Wno-stringop-overflow" } */ + +int main (void) +{ + char a[8] = ""; + + __builtin_strcpy (a, "123"); + + unsigned n0 = __builtin_strlen (a); + + __builtin_strncpy (a + 3, a, n0); + + unsigned n1 = __builtin_strlen (a); + + if (n1 == n0) + __builtin_abort (); +} diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index 48b9241..94f20ef 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -1941,10 +1941,9 @@ handle_builtin_stxncpy (built_in_function, gimple_stmt_iterator *gsi) int sidx = get_stridx (src); strinfo *sisrc = sidx > 0 ? get_strinfo (sidx) : NULL; - /* Strncpy() et al. cannot modify the source string. Prevent the rest - of the pass from invalidating the strinfo data. */ - if (sisrc) - sisrc->dont_invalidate = true; + /* strncat() and strncpy() can modify the source string by writing + over the terminating nul so SISRC->DONT_INVALIDATE must be left + clear. */ /* Retrieve the strinfo data for the string S that LEN was computed from as some function F of strlen (S) (i.e., LEN need not be equal |