[clang] [Clang] Fixed an assertion when ``__attribute__((alloc_size))`` is ued with an argument type wider than the target's pointer width (PR #202381)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 8 09:20:21 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: TPPPP (TPPPP72)
<details>
<summary>Changes</summary>
Replace `zext()` with `extOrTrunc()` when normalizing the `APInt` value to `BitsInSizeT`.
fix #<!-- -->190445
---
Full diff: https://github.com/llvm/llvm-project/pull/202381.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/AST/Expr.cpp (+1-1)
- (modified) clang/test/Sema/warn-alloc-size.c (+12)
``````````diff
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
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/202381
More information about the cfe-commits
mailing list