From: Martin Sebor Date: Wed, 6 Dec 2017 17:47:45 +0000 (+0000) Subject: PR tree-optimization/83075 - Invalid strncpy optimization X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=25b15e953ac48e5042e27a4419ca20211403b7cb;p=gcc.git 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 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ce17016743c..5bcc6a9580b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2017-12-06 Martin Sebor + + 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 Revert diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 9fef2d5bca4..c39f3d260c6 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2017-12-06 Martin Sebor + + 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 * 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 00000000000..93a60c8fd1a --- /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 00000000000..42203d5cda8 --- /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 48b92417b57..94f20efb4a1 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