https://github.com/TPPPP72 created 
https://github.com/llvm/llvm-project/pull/202381

Replace `zext()` with `extOrTrunc()` when normalizing the `APInt` value to 
`BitsInSizeT`.

fix #190445 

>From 1d9c6879580e331bb208127bd60d9c6ea7680e03 Mon Sep 17 00:00:00 2001
From: TPPPP <[email protected]>
Date: Tue, 9 Jun 2026 00:15:31 +0800
Subject: [PATCH] [Clang] Fixed an assertion when
 ``__attribute__((alloc_size))`` is used with an argument type wider than the
 target's pointer width

---
 clang/docs/ReleaseNotes.rst       |  1 +
 clang/lib/AST/Expr.cpp            |  2 +-
 clang/test/Sema/warn-alloc-size.c | 12 ++++++++++++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index aebd60e1646d6..86b2c086ee956 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -672,6 +672,7 @@ Bug Fixes in This Version
 - Fixed a crash when ``#embed`` is used with C++ modules (#GH195350)
 - Fixed an issue where ``__typeof_unqual`` and ``__typeof_unqual__`` were 
rejected as a declaration specifier in block scope in C++.
 - Fixed crash when checking for overflow for unary operator that can't 
overflow (#GH170072)
+- Fixed an assertion when ``__attribute__((alloc_size))`` is used with an 
argument type wider than the target's pointer width. (#GH190445)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 90747be4208e1..babb30750e41b 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -3622,7 +3622,7 @@ CallExpr::evaluateBytesReturnedByAllocSizeCall(const 
ASTContext &Ctx) const {
     Into = ExprResult.Val.getInt();
     if (Into.isNegative() || !Into.isIntN(BitsInSizeT))
       return false;
-    Into = Into.zext(BitsInSizeT);
+    Into = Into.extOrTrunc(BitsInSizeT);
     return true;
   };
 
diff --git a/clang/test/Sema/warn-alloc-size.c 
b/clang/test/Sema/warn-alloc-size.c
index 3d403610c46f9..6000d04b8d298 100644
--- a/clang/test/Sema/warn-alloc-size.c
+++ b/clang/test/Sema/warn-alloc-size.c
@@ -1,13 +1,24 @@
 // RUN: %clang_cc1 -triple x86_64-linux -fsyntax-only -verify -Walloc-size %s
+// RUN: %clang_cc1 -triple x86_64-linux -fsyntax-only -verify=int128 
-Walloc-size %s -DTEST_INT128
 struct Foo { int x[10]; };
 
+#ifdef TEST_INT128
+typedef __typeof__(__int128) size_t;
+#else
 typedef __typeof__(sizeof(int)) size_t;
+#endif
+
 void *my_malloc(size_t) __attribute__((alloc_size(1)));
 void *my_calloc(size_t, size_t) __attribute__((alloc_size(2, 1)));
 
 void foo_consumer(struct Foo* p);
 
 void alloc_foo(void) {
+#ifdef TEST_INT128
+  struct Foo *ptr = (struct Foo *)my_malloc(sizeof(*ptr));
+
+  struct Foo *ptr_wrong = (struct Foo *)my_malloc(4); // 
int128-warning{{allocation of insufficient size '4' for type 'struct Foo' with 
size '40'}}
+#else
   struct Foo *ptr1 = my_malloc(sizeof(struct Foo));
   struct Foo *ptr2 = my_malloc(sizeof(*ptr2));
   struct Foo *ptr3 = my_calloc(1, sizeof(*ptr3));
@@ -43,4 +54,5 @@ void alloc_foo(void) {
   // Zero size allocations are assumed to be intentional.
   int *zero_alloc1 = my_malloc(0);
   int *zero_alloc2 = (int *)my_malloc(0);
+  #endif
 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to