[clang] [Clang] Fix offsetof sign-extending unsigned array indices >= 128 (PR #204139)
Marlus Cadanus da Costa via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 17 07:17:52 PDT 2026
================
@@ -6544,6 +6544,14 @@ bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E,
// When generating bytecode, we put all the index expressions as Sint64 on
// the stack.
int64_t Index = ArrayIndices[ArrayIndex];
+ // Reject negative indices and unsigned indices that wrapped to negative
+ // after the Uint64->Sint64 cast (e.g. __uint128_t >= 0x8000000000000000).
+ if (Index < 0) {
----------------
marlus wrote:
Thanks. The Index < 0 guard here doesn't cover values that overflow 64 bits entirely — for example, __uint128_t(2^64) gets silently truncated to 0 when cast through PT_Uint64 in Compiler.cpp, so Index arrives as 0 and bypasses the guard.
Fixed in Compiler.cpp: AP types (PT_IntAP / PT_IntAPS, i.e. __uint128_t, __int128, etc.) now cause constant evaluation to fail before any bytecode is emitted, avoiding the truncation entirely. Added a test for ((__uint128_t)1 << 64) to cover this case explicitly.
https://github.com/llvm/llvm-project/pull/204139
More information about the cfe-commits
mailing list