strlen: do not use cond_expr for boundaries

Message ID 677e6b2b-5198-f2b2-d5e5-dc388b77bcb0@suse.cz
State Accepted
Headers
Series strlen: do not use cond_expr for boundaries |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Martin Liška Dec. 23, 2022, 3:44 p.m. UTC
  Hi.

We reach cond_expr and then we get an ICE in tree_int_cst_lt.
Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

	PR tree-optimization/108137

gcc/ChangeLog:

	* tree-ssa-strlen.cc (get_range_strlen_phi): Reject anything
	different from INTEGER_CST.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/pr108137.c: New test.
---
 gcc/testsuite/gcc.dg/tree-ssa/pr108137.c |  8 ++++++++
 gcc/tree-ssa-strlen.cc                   | 13 +++++++------
 2 files changed, 15 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr108137.c
  

Comments

Jeff Law Dec. 27, 2022, 5:38 p.m. UTC | #1
On 12/23/22 08:44, Martin Liška wrote:
> Hi.
> 
> We reach cond_expr and then we get an ICE in tree_int_cst_lt.
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> Ready to be installed?
> Thanks,
> Martin
> 
> 	PR tree-optimization/108137
> 
> gcc/ChangeLog:
> 
> 	* tree-ssa-strlen.cc (get_range_strlen_phi): Reject anything
> 	different from INTEGER_CST.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.dg/tree-ssa/pr108137.c: New test.
OK.  You could perhaps do better by looking at the arms of the 
COND_EXPR, but this seems like the safest thing to do for gcc-13.

jeff
  

Patch

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr108137.c b/gcc/testsuite/gcc.dg/tree-ssa/pr108137.c
new file mode 100644
index 00000000000..f0cb71b2267
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr108137.c
@@ -0,0 +1,8 @@ 
+// PR tree-optimization/108137
+// { dg-do compile }
+// { dg-options "-Wformat-overflow" }
+
+void f(unsigned short x_port, unsigned int x_host)
+{
+    __builtin_printf("missing %s", x_port ? "host" : &"host:port"[x_host ? 5 : 0]);
+}
diff --git a/gcc/tree-ssa-strlen.cc b/gcc/tree-ssa-strlen.cc
index abec225566d..a2edac4c77f 100644
--- a/gcc/tree-ssa-strlen.cc
+++ b/gcc/tree-ssa-strlen.cc
@@ -1136,14 +1136,15 @@  get_range_strlen_phi (tree src, gphi *phi,
 
       /* Adjust the minimum and maximum length determined so far and
 	 the upper bound on the array size.  */
-      if (!pdata->minlen
-	  || tree_int_cst_lt (argdata.minlen, pdata->minlen))
+      if (TREE_CODE (argdata.minlen) == INTEGER_CST
+	  && (!pdata->minlen
+	      || tree_int_cst_lt (argdata.minlen, pdata->minlen)))
 	pdata->minlen = argdata.minlen;
 
-      if (!pdata->maxlen
-	  || (argdata.maxlen
-	      && TREE_CODE (argdata.maxlen) == INTEGER_CST
-	      && tree_int_cst_lt (pdata->maxlen, argdata.maxlen)))
+      if (TREE_CODE (argdata.maxlen) == INTEGER_CST
+	  && (!pdata->maxlen
+	      || (argdata.maxlen
+		  && tree_int_cst_lt (pdata->maxlen, argdata.maxlen))))
 	pdata->maxlen = argdata.maxlen;
 
       if (!pdata->maxbound