diff mbox series

adjust strnlen to convert MIN_EXPR to the same type (PR 87112)

Message ID a69a56de-7c84-eb31-ceaa-dbdc550bffa6@gmail.com
State New
Headers show
Series adjust strnlen to convert MIN_EXPR to the same type (PR 87112) | expand

Commit Message

Martin Sebor Aug. 27, 2018, 10:57 p.m. UTC
The assertion I added in the fix for PR 87059 to help find
mismatched MIN/MAX_EXPR operands has exposed another instance
of such a mismatch, this one in expand_builtin_strnlen().

The attached patch adjusts the function to convert the signed
result of c_strlen() to the unsigned type of the second strnlen
argument.

Martin

Comments

Jeff Law Aug. 27, 2018, 11:13 p.m. UTC | #1
On 08/27/2018 04:57 PM, Martin Sebor wrote:
> The assertion I added in the fix for PR 87059 to help find
> mismatched MIN/MAX_EXPR operands has exposed another instance
> of such a mismatch, this one in expand_builtin_strnlen().
> 
> The attached patch adjusts the function to convert the signed
> result of c_strlen() to the unsigned type of the second strnlen
> argument.
> 
> Martin
> 
> gcc-87112.diff
> 
> 
> PR tree-optimization/87112 - ICE in fold_binary_loc on strnlen of mixed integer types
> 
> gcc/ChangeLog:
> 
> 	PR tree-optimization/87112
> 	* builtins.c (expand_builtin_strnlen): Convert c_strlen result to
> 	the type of the bound argument.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR tree-optimization/87112
> 	* gcc.dg/pr87112.c: New test.
OK.
jeff
diff mbox series

Patch

PR tree-optimization/87112 - ICE in fold_binary_loc on strnlen of mixed integer types

gcc/ChangeLog:

	PR tree-optimization/87112
	* builtins.c (expand_builtin_strnlen): Convert c_strlen result to
	the type of the bound argument.

gcc/testsuite/ChangeLog:

	PR tree-optimization/87112
	* gcc.dg/pr87112.c: New test.

Index: gcc/builtins.c
===================================================================
--- gcc/builtins.c	(revision 263896)
+++ gcc/builtins.c	(working copy)
@@ -2970,6 +2970,10 @@  expand_builtin_strnlen (tree exp, rtx target, mach
   tree func = get_callee_fndecl (exp);
 
   tree len = c_strlen (src, 0);
+  /* FIXME: Change c_strlen() to return sizetype instead of ssizetype
+     so these conversions aren't necessary.  */
+  if (len)
+    len = fold_convert_loc (loc, TREE_TYPE (bound), len);
 
   if (TREE_CODE (bound) == INTEGER_CST)
     {
@@ -2984,7 +2988,6 @@  expand_builtin_strnlen (tree exp, rtx target, mach
       if (!len || TREE_CODE (len) != INTEGER_CST)
 	return NULL_RTX;
 
-      len = fold_convert_loc (loc, size_type_node, len);
       len = fold_build2_loc (loc, MIN_EXPR, size_type_node, len, bound);
       return expand_expr (len, target, target_mode, EXPAND_NORMAL);
     }
Index: gcc/testsuite/gcc.dg/pr87112.c
===================================================================
--- gcc/testsuite/gcc.dg/pr87112.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/pr87112.c	(working copy)
@@ -0,0 +1,31 @@ 
+/* PR tree-optimization/87112 - ICE due to strnlen mixing integer types
+   { dg-do compile }
+   { dg-options "-Os -Wall" } */
+
+typedef __SIZE_TYPE__ size_t;
+
+extern size_t strnlen (const char*, size_t);
+
+size_t fi (int i)
+{
+  int n = i & 3;
+  return strnlen ("int", n);
+}
+
+size_t fui (unsigned i)
+{
+  unsigned n = i & 3;
+  return strnlen ("unsigned", n);
+}
+
+size_t fl (long i)
+{
+  long n = i & 3;
+  return strnlen ("long", n);
+}
+
+size_t fsz (size_t i)
+{
+  size_t n = i & 3;
+  return strnlen ("size_t", n);
+}