PR tree-optimization/83075 - Invalid strncpy optimization
authorMartin Sebor <msebor@redhat.com>
Wed, 6 Dec 2017 17:47:45 +0000 (17:47 +0000)
committerMartin Sebor <msebor@gcc.gnu.org>
Wed, 6 Dec 2017 17:47:45 +0000 (10:47 -0700)
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

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/strncat.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/strncpy-2.c [new file with mode: 0644]
gcc/tree-ssa-strlen.c

index ce17016743c88fc4412a3a7e7294c6ded5ec1f58..5bcc6a9580b3bba0268bd5ea4ad7cbae3ea4a09f 100644 (file)
@@ -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
index 9fef2d5bca4f5892383dfd0d30db09dc90b02ce7..c39f3d260c6386664a240592557ca5130c0c88a2 100644 (file)
@@ -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 (file)
index 0000000..93a60c8
--- /dev/null
@@ -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 (file)
index 0000000..42203d5
--- /dev/null
@@ -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 ();
+}
index 48b92417b57411d5f91e1ed751005d927ece4ea5..94f20efb4a1ae152bfa4ad0631fba096a26544f3 100644 (file)
@@ -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