[PATCH] Fix ICE in strlen () > 0 folding (PR tree-optimization/89314)

Jakub Jelinek jakub@redhat.com
Tue Feb 12 23:14:00 GMT 2019


Hi!

fold_binary_loc verifies that strlen argument is a pointer, but doesn't
verify what the pointee is.
The following patch just always converts it to the right pointer type
(const char *) and dereferences only that.
Another option would be punt if the pointee (TYPE_MAIN_VARIANT) is not
char_type_node, but then e.g. unsigned_char_type_node or
signed_char_type_node (or maybe char8_t) wouldn't be that bad.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2019-02-12  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/89314
	* fold-const.c (fold_binary_loc): Cast strlen argument to
	const char * before dereferencing it.  Formatting fixes.

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

--- gcc/fold-const.c.jj	2019-02-11 18:04:18.000000000 +0100
+++ gcc/fold-const.c	2019-02-12 21:11:21.491388038 +0100
@@ -10740,20 +10740,24 @@ fold_binary_loc (location_t loc, enum tr
 		strlen(ptr) != 0   =>  *ptr != 0
 	 Other cases should reduce to one of these two (or a constant)
 	 due to the return value of strlen being unsigned.  */
-      if (TREE_CODE (arg0) == CALL_EXPR
-	  && integer_zerop (arg1))
+      if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
 	{
 	  tree fndecl = get_callee_fndecl (arg0);
 
 	  if (fndecl
 	      && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
 	      && call_expr_nargs (arg0) == 1
-	      && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
+	      && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
+		  == POINTER_TYPE))
 	    {
-	      tree iref = build_fold_indirect_ref_loc (loc,
-						   CALL_EXPR_ARG (arg0, 0));
+	      tree ptrtype
+		= build_pointer_type (build_qualified_type (char_type_node,
+							    TYPE_QUAL_CONST));
+	      tree ptr = fold_convert_loc (loc, ptrtype,
+					   CALL_EXPR_ARG (arg0, 0));
+	      tree iref = build_fold_indirect_ref_loc (loc, ptr);
 	      return fold_build2_loc (loc, code, type, iref,
-				  build_int_cst (TREE_TYPE (iref), 0));
+				      build_int_cst (TREE_TYPE (iref), 0));
 	    }
 	}
 
--- gcc/testsuite/gcc.dg/pr89314.c.jj	2019-02-12 21:15:11.624589045 +0100
+++ gcc/testsuite/gcc.dg/pr89314.c	2019-02-12 21:14:49.138960233 +0100
@@ -0,0 +1,13 @@
+/* PR tree-optimization/89314 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wbuiltin-declaration-mismatch -Wextra" } */
+
+extern __SIZE_TYPE__ strlen (const float *);	/* { dg-warning "mismatch in argument 1 type of built-in function" } */
+void bar (void);
+
+void
+foo (float *s)
+{
+  if (strlen (s) > 0)
+    bar ();
+}

	Jakub



More information about the Gcc-patches mailing list