[clang] da252a8 - [clang][bytecode] Allow pointer type mismatch in SubPtr op (#222940)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 11 07:41:22 PDT 2026
Author: Timm Baeder
Date: 2026-09-11T16:41:17+02:00
New Revision: da252a8b6fb505e2395bbb01c2e62037dae95eb3
URL: https://github.com/llvm/llvm-project/commit/da252a8b6fb505e2395bbb01c2e62037dae95eb3
DIFF: https://github.com/llvm/llvm-project/commit/da252a8b6fb505e2395bbb01c2e62037dae95eb3.diff
LOG: [clang][bytecode] Allow pointer type mismatch in SubPtr op (#222940)
The result of the attached test case is 1, which then gets divided by 8
and the end result is 0. This should evaluate.
Added:
Modified:
clang/lib/AST/ByteCode/Interp.h
clang/test/AST/ByteCode/invalid.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 343883872728b..a20a3f91dd267 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2876,7 +2876,8 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC, uint32_t ElemSize) {
if (!VR)
return false;
- assert(((int64_t)*VL - (int64_t)*VR) % ElemSize == 0);
+ // We allow (VL - VR) / Elemsize to have non-zero remainder. This happens for
+ // invalid expressions where LHS and RHS are of
diff erent types.
int64_t R64 =
(static_cast<int64_t>(*VL) - static_cast<int64_t>(*VR)) / ElemSize;
if (static_cast<int64_t>(T::from(R64)) != R64)
diff --git a/clang/test/AST/ByteCode/invalid.cpp b/clang/test/AST/ByteCode/invalid.cpp
index 67c82cb352d90..2e822e0b56d91 100644
--- a/clang/test/AST/ByteCode/invalid.cpp
+++ b/clang/test/AST/ByteCode/invalid.cpp
@@ -256,3 +256,14 @@ namespace UnknownSizeArrayInEvaluateString {
}
}
} // both-error {{extraneous closing brace}}
+
+namespace SubPtrResultIs1 {
+ struct A {
+ char x;
+ };
+ struct B {
+ char y;
+ };
+ struct C : A, B {};
+ unsigned char x = ((char **)(B *)(C *)0x1000) - (char *)0x1000; // both-error {{not pointers to compatible types}}
+}
More information about the cfe-commits
mailing list