]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
strlen: Fix up !si->full_string_p handling in count_nonzero_bytes_addr [PR115152]
authorJakub Jelinek <jakub@redhat.com>
Wed, 22 May 2024 07:13:50 +0000 (09:13 +0200)
committerJakub Jelinek <jakub@redhat.com>
Wed, 22 May 2024 07:13:50 +0000 (09:13 +0200)
The following testcase is miscompiled because
strlen_pass::count_nonzero_bytes_addr doesn't handle correctly
the !si->full_string_p case.
If si->full_string_p, it correctly computes minlen and maxlen as
minimum and maximum length of the '\0' terminated stgring and
clears *nulterm (ie. makes sure !full_string_p in the ultimate
caller) if minlen is equal or larger than nbytes and so
'\0' isn't guaranteed to be among those bytes.
But in the !si->full_string_p case, all we know is that there
are [minlen,maxlen] non-zero bytes followed by unknown bytes,
so effectively the maxlen is infinite (but caller cares about only
the first nbytes bytes) and furthermore, we never know if there is
any '\0' char among those, so *nulterm needs to be always cleared.

2024-05-22  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/115152
* tree-ssa-strlen.cc (strlen_pass::count_nonzero_bytes_addr): If
!si->full_string_p, clear *nulterm and set maxlen to nbytes.

* gcc.dg/pr115152.c: New test.

gcc/testsuite/gcc.dg/pr115152.c [new file with mode: 0644]
gcc/tree-ssa-strlen.cc

diff --git a/gcc/testsuite/gcc.dg/pr115152.c b/gcc/testsuite/gcc.dg/pr115152.c
new file mode 100644 (file)
index 0000000..a44654e
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR tree-optimization/115152 */
+/* { dg-do run } */
+/* { dg-options "-O3 -fno-tree-fre -fno-tree-dominator-opts -fno-tree-loop-im" } */
+
+int a, b, c, d;
+signed char e[1] = { 1 };
+
+int
+main ()
+{
+  for (a = 0; a < 3; a++)
+    for (b = 0; b < 2; b++)
+      c = e[0] = e[0] ^ d;
+  if (!c)
+    __builtin_abort ();
+  return 0;
+}
index 61c3da22322cfed7bd1d8c1884f63c77af2620e1..7596dd809427288996d0fa5fe6bb92f9ec960b00 100644 (file)
@@ -4829,7 +4829,7 @@ strlen_pass::count_nonzero_bytes_addr (tree exp, tree vuse, gimple *stmt,
       if (maxlen + 1 < nbytes)
        return false;
 
-      if (nbytes <= minlen)
+      if (nbytes <= minlen || !si->full_string_p)
        *nulterm = false;
 
       if (nbytes < minlen)
@@ -4839,6 +4839,9 @@ strlen_pass::count_nonzero_bytes_addr (tree exp, tree vuse, gimple *stmt,
            maxlen = nbytes;
        }
 
+      if (!si->full_string_p)
+       maxlen = nbytes;
+
       if (minlen < lenrange[0])
        lenrange[0] = minlen;
       if (lenrange[1] < maxlen)