c: ICE in gimplify_expr when counted_by for pointer is added [PR122982]

Message ID 20251205201054.2995808-1-qing.zhao@oracle.com
State Committed
Commit cd4d3a14b231cd5a0e38034e1aa155078944bc8e
Headers
Series c: ICE in gimplify_expr when counted_by for pointer is added [PR122982] |

Commit Message

Qing Zhao Dec. 5, 2025, 8:10 p.m. UTC
  The first argument of the call to .ACCESS_WITH_SIZE includes
"c_maybe_const_expr" which should not be passed to gimplifier.

Before passing the expression as the first argument to the call to
.ACCESS_WITH_SIZE, c_fully_fold should be called on this expression.

Bootstrappped and regression tested on both x86 and aarch64. no issues.

Okay for committing to trunk?

Thanks.

Qimg
==================
	PR c/122982

gcc/c/ChangeLog:

	* c-typeck.cc (build_access_with_size_for_counted_by): Call
	c_fully_fold on the first parameter.

gcc/testsuite/ChangeLog:

	* gcc.dg/pointer-counted-by-pr122982.c: New test.
---
 gcc/c/c-typeck.cc                             |  2 +-
 .../gcc.dg/pointer-counted-by-pr122982.c      | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/pointer-counted-by-pr122982.c
  

Comments

Joseph Myers Dec. 8, 2025, 11:28 p.m. UTC | #1
On Fri, 5 Dec 2025, Qing Zhao wrote:

> The first argument of the call to .ACCESS_WITH_SIZE includes
> "c_maybe_const_expr" which should not be passed to gimplifier.
> 
> Before passing the expression as the first argument to the call to
> .ACCESS_WITH_SIZE, c_fully_fold should be called on this expression.
> 
> Bootstrappped and regression tested on both x86 and aarch64. no issues.
> 
> Okay for committing to trunk?

OK.
  

Patch

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index a34ca2ab97c..13bba01e442 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -3187,7 +3187,7 @@  build_access_with_size_for_counted_by (location_t loc, tree ref,
   tree first_param = is_fam
 		     ? c_fully_fold (array_to_pointer_conversion (loc, ref),
 				     false, NULL)
-		     : ref;
+		     : c_fully_fold (ref, false, NULL);
   tree second_param
     = c_fully_fold (counted_by_ref, false, NULL);
   tree third_param = build_int_cst (c_build_pointer_type (counted_by_type), 0);
diff --git a/gcc/testsuite/gcc.dg/pointer-counted-by-pr122982.c b/gcc/testsuite/gcc.dg/pointer-counted-by-pr122982.c
new file mode 100644
index 00000000000..1bad7f080e0
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pointer-counted-by-pr122982.c
@@ -0,0 +1,19 @@ 
+/* PR c/122982 */ 
+/* { dg-do compile } */
+/* { dg-options "-O0" } */
+
+int* f (int);
+
+struct __bounded_ptr {
+ int k;
+ int *buf __attribute__ ((counted_by (k)));
+};
+
+int*
+f1 (int n) { return f (n); }
+
+void h1 (void)
+{ 
+  int *p = (struct __bounded_ptr) {3, f1 (3)}.buf;
+  __builtin_memset (p, 0, 3 * sizeof p);
+}