[clang] [clang][bytecode] Allow pointer type mismatch in SubPtr op (PR #222940)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 11 05:58:43 PDT 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/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.
>From a7cbb5d3b59a1f97f2d3f0979d190c476a9c21fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 11 Sep 2026 14:53:03 +0200
Subject: [PATCH] [clang][bytecode] Allow pointer type mismatch in SubPtr op
The result of the attached test case is 1, which then gets divided by 8
and the end result is 0. This should evaluate.
---
clang/lib/AST/ByteCode/Interp.h | 3 ++-
clang/test/AST/ByteCode/invalid.cpp | 11 +++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 61118d77b7ac2..72eea07392703 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2847,7 +2847,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 different 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