]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end/119204 - ICE with strcspn folding
authorRichard Biener <rguenther@suse.de>
Tue, 11 Mar 2025 07:40:44 +0000 (08:40 +0100)
committerRichard Biener <rguenth@gcc.gnu.org>
Tue, 11 Mar 2025 11:05:01 +0000 (12:05 +0100)
The following makes sure to convert the folded expression to the
original expression type.

PR middle-end/119204
* builtins.cc (fold_builtin_strcspn): Preserve the original
expression type.

* gcc.dg/pr119204.c: New testcase.

gcc/builtins.cc
gcc/testsuite/gcc.dg/pr119204.c [new file with mode: 0644]

index c8841032f03997d50d75a09cd74b0f3f2c2ab6eb..02556e6615ad63c0c77901d529dbcd359fe77a68 100644 (file)
@@ -11360,7 +11360,7 @@ fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2)
     {
       /* Evaluate and ignore argument s2 in case it has
         side-effects.  */
-      return omit_one_operand_loc (loc, size_type_node,
+      return omit_one_operand_loc (loc, TREE_TYPE (expr),
                                   size_zero_node, s2);
     }
 
@@ -11375,7 +11375,8 @@ fold_builtin_strcspn (location_t loc, tree expr, tree s1, tree s2)
       if (!fn)
        return NULL_TREE;
 
-      return build_call_expr_loc (loc, fn, 1, s1);
+      return fold_convert_loc (loc, TREE_TYPE (expr),
+                              build_call_expr_loc (loc, fn, 1, s1));
     }
   return NULL_TREE;
 }
diff --git a/gcc/testsuite/gcc.dg/pr119204.c b/gcc/testsuite/gcc.dg/pr119204.c
new file mode 100644 (file)
index 0000000..ecbd8dd
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-w" } */
+
+extern void abort(void);
+extern long long strcspn(const char *, const char *);
+
+void main_test(void) {
+  const char *const s1 = "hello world";
+  char dst[64], *d2;
+
+  if (strcspn(++d2 + 5, "") != 5 || d2 != dst + 1)
+    abort();
+}