]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Fix ICEs with -fsanitize=pointer-{subtract,compare} [PR119582]
authorJakub Jelinek <jakub@redhat.com>
Wed, 2 Apr 2025 17:28:20 +0000 (19:28 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 2 Apr 2025 17:28:20 +0000 (19:28 +0200)
The following testcase ICEs because c_fully_fold isn't performed on the
arguments of __sanitizer_ptr_{sub,cmp} builtins and so e.g.
C_MAYBE_CONST_EXPR can leak into the gimplifier where it ICEs.

2025-04-02  Jakub Jelinek  <jakub@redhat.com>

PR c/119582
* c-typeck.cc (pointer_diff, build_binary_op): Call c_fully_fold on
__sanitizer_ptr_sub or __sanitizer_ptr_cmp arguments.

* gcc.dg/asan/pr119582.c: New test.

gcc/c/c-typeck.cc
gcc/testsuite/gcc.dg/asan/pr119582.c [new file with mode: 0644]

index aaf8e54416a2bd9423c62889db597a098909e793..19e79b554dca792cbb623381c1a96726de55382d 100644 (file)
@@ -4824,8 +4824,8 @@ pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
   if (current_function_decl != NULL_TREE
       && sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
     {
-      op0 = save_expr (op0);
-      op1 = save_expr (op1);
+      op0 = save_expr (c_fully_fold (op0, false, NULL));
+      op1 = save_expr (c_fully_fold (op1, false, NULL));
 
       tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
       *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
@@ -14455,8 +14455,8 @@ build_binary_op (location_t location, enum tree_code code,
          && current_function_decl != NULL_TREE
          && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
        {
-         op0 = save_expr (op0);
-         op1 = save_expr (op1);
+         op0 = save_expr (c_fully_fold (op0, false, NULL));
+         op1 = save_expr (c_fully_fold (op1, false, NULL));
 
          tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
          instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
diff --git a/gcc/testsuite/gcc.dg/asan/pr119582.c b/gcc/testsuite/gcc.dg/asan/pr119582.c
new file mode 100644 (file)
index 0000000..f33cb51
--- /dev/null
@@ -0,0 +1,23 @@
+/* PR c/119582 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fsanitize=address,pointer-subtract,pointer-compare" } */
+
+const char v;
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
+char a;
+const ptrdiff_t p = &a + 1 - &a;
+const int q = (&a + 1) != &a;
+
+ptrdiff_t
+foo (void)
+{
+  char b;
+  return &b + (v != '\n') - &b;
+}
+
+int
+bar (void)
+{
+  char b;
+  return (&b + (v != '\n')) != &b;
+}