On Wed, Dec 06, 2017 at 05:30:53PM +0100, Jakub Jelinek wrote:
> On Wed, Dec 06, 2017 at 09:20:15AM -0700, Martin Sebor wrote:
> > Attached is a patch with the comment updated/simplified.
> > The tests do the job they need to do today so I just removed
> > the useless attribute but otherwise left them unchanged.  If
> > you would like to enhance them in some way please feel free.
> 
> Ok for trunk, with a minor nit.  I'll tweak the tests incrementally
> when it is in.

So here is the fix for those testcases.

They didn't test what they meant to test, because they didn't FAIL
without the patch.  That is because the bug was that the -W* option
affected code generation, so with -O2 -Wno-stringop-overflow it didn't
trigger it.
I've changed the tests to test both in a separate noipa function where
it doesn't know about the aliasing and string lengths from the caller,
in that case it does more verifications, including the content of the
whole buffer, and the individual values of the lengths,
and what you did before.

Regtested on x86_64-linux and i686-linux, verified that with the
r255446 tree-ssa-strlen.c change reverted it FAILs.

Ok for trunk?

2017-12-07  Jakub Jelinek  <ja...@redhat.com>

        PR tree-optimization/83075
        * gcc.dg/tree-ssa/strncpy-2.c: Use size_t instead of unsigned, add
        separate function with noipa attribute to also verify behavior when
        optimizers don't know the sizes and aliasing, verify resulting sizes
        and array content.  Add -Wstringop-overflow to dg-options.
        * gcc.dg/tree-ssa/strncat.c: Likewise.

--- gcc/testsuite/gcc.dg/tree-ssa/strncpy-2.c.jj        2017-12-06 
20:11:54.000000000 +0100
+++ gcc/testsuite/gcc.dg/tree-ssa/strncpy-2.c   2017-12-07 13:31:32.719722416 
+0100
@@ -1,19 +1,35 @@
-/* PR tree-optimization/83075 - Invalid strncpy optimization
-   { dg-do run }
-   { dg-options "-O2 -Wno-stringop-overflow" } */
+/* PR tree-optimization/83075 - Invalid strncpy optimization */
+/* { dg-do run } */
+/* { dg-options "-O2 -Wstringop-overflow" } */
 
-int main (void)
+typedef __SIZE_TYPE__ size_t;
+
+__attribute__((noipa)) size_t
+foo (char *p, char *q, size_t *r)
 {
-  char a[8] = "";
+  size_t n0 = __builtin_strlen (p);
+  __builtin_strncpy (q, p, n0);                /* { dg-warning "specified 
bound depends on the length" } */
+  size_t n1 = __builtin_strlen (p);
+  *r = n0;
+  return n1;
+}
 
+int
+main ()
+{
+  char a[8] = "";
   __builtin_strcpy (a, "123");
-
-  unsigned n0 = __builtin_strlen (a);
-
-  __builtin_strncpy (a + 3, a, n0);
-
-  unsigned n1 = __builtin_strlen (a);
-
+  size_t n0 = __builtin_strlen (a);
+  __builtin_strncpy (a + 3, a, n0);    /* { dg-warning "specified bound 
depends on the length" } */
+  size_t n1 = __builtin_strlen (a);
   if (n1 == n0)
     __builtin_abort ();
+  a[6] = '7';
+  __builtin_strcpy (a, "456");
+  size_t n2;
+  if (foo (a, a + 3, &n2) != 7 || n2 != 3)
+    __builtin_abort ();
+  if (__builtin_memcmp (a, "4564567", sizeof "4564567"))
+    __builtin_abort ();
+  return 0;
 }
--- gcc/testsuite/gcc.dg/tree-ssa/strncat.c.jj  2017-12-06 20:11:54.000000000 
+0100
+++ gcc/testsuite/gcc.dg/tree-ssa/strncat.c     2017-12-07 13:31:09.568008365 
+0100
@@ -1,19 +1,35 @@
-/* PR tree-optimization/83075 - Invalid strncpy optimization
-   { dg-do run }
-   { dg-options "-O2 -Wno-stringop-overflow" } */
+/* PR tree-optimization/83075 - Invalid strncpy optimization */
+/* { dg-do run } */
+/* { dg-options "-O2 -Wstringop-overflow" } */
 
-int main (void)
+typedef __SIZE_TYPE__ size_t;
+
+__attribute__((noipa)) size_t
+foo (char *p, char *q, size_t *r)
 {
-  char a[8] = "";
+  size_t n0 = __builtin_strlen (p);
+  __builtin_strncat (q, p, n0);                /* { dg-warning "specified 
bound depends on the length" } */
+  size_t n1 = __builtin_strlen (p);
+  *r = n0;
+  return n1;
+}
 
+int
+main ()
+{
+  char a[8] = "";
   __builtin_strcpy (a, "123");
-
-  unsigned n0 = __builtin_strlen (a);
-
-  __builtin_strncat (a + 3, a, n0);
-
-  unsigned n1 = __builtin_strlen (a);
-
+  size_t n0 = __builtin_strlen (a);
+  __builtin_strncat (a + 3, a, n0);    /* { dg-warning "specified bound 
depends on the length" } */
+  size_t n1 = __builtin_strlen (a);
   if (n1 == n0)
     __builtin_abort ();
+  a[6] = '7';
+  __builtin_strcpy (a, "456");
+  size_t n2;
+  if (foo (a, a + 3, &n2) != 6 || n2 != 3)
+    __builtin_abort ();
+  if (__builtin_memcmp (a, "456456\0", sizeof "456456\0"))
+    __builtin_abort ();
+  return 0;
 }


        Jakub

Reply via email to