[clang] [clang][bytecode] Fix null ptr upcasts adding offset (PR #224873)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 19 17:14:18 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Benji Smith (Benjins)
<details>
<summary>Changes</summary>
Previously, this was applying the offset it would for non-null pointers, but null pointers should always cast to a null pointer result.
Fixes https://github.com/llvm/llvm-project/issues/224869
---
Full diff: https://github.com/llvm/llvm-project/pull/224873.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+3)
- (modified) clang/lib/AST/ByteCode/Pointer.cpp (+4)
- (modified) clang/test/AST/ByteCode/const-base-cast.cpp (+5)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index a1f24a8caedae..684b02fb0af1a 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -727,6 +727,9 @@ features cannot lower the translation-unit ABI level;
- Fixed ambiguous overload where two non-static member functions with
different signatures could be incorrectly considered equivalent. (#GH224499)
+- Fixed the bytecode interpreter evaluating upcasted null pointers to a
+ non-null pointer. (#GH224869)
+
#### Bug Fixes to AST Handling
- Fixed a non-deterministic ordering of unused local typedefs that made
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index dca2630138a4e..fb05efe5a62c3 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -1373,6 +1373,10 @@ IntPointer IntPointer::baseCast(const interp::Context &Ctx,
if (CurType.isNull() || !CurType->isRecordType())
return *this;
+ // null pointers stay null during a cast, per conv.ptr
+ if (Value == 0)
+ return *this;
+
const Record *R = Ctx.getRecord(CurType->getAsRecordDecl());
// This iterates over bases and checks for the proper offset. That's
diff --git a/clang/test/AST/ByteCode/const-base-cast.cpp b/clang/test/AST/ByteCode/const-base-cast.cpp
index 80226b973bf97..696eff5959cc4 100644
--- a/clang/test/AST/ByteCode/const-base-cast.cpp
+++ b/clang/test/AST/ByteCode/const-base-cast.cpp
@@ -17,3 +17,8 @@ unsigned char y = ((char*)(B*)(C*)0x1000) - (char*)0x1000;
unsigned char z = ((char*)(A*)(C*)0x1000) - (char*)0x1000;
// CHECK: @z = {{(dso_local )?}}global i8 0
+unsigned char n0 = ((char*)(B*)(C*)0) - (char*)0;
+// CHECK: @n0 = {{(dso_local )?}}global i8 0
+
+unsigned char n1 = ((char*)(A*)(C*)0) - (char*)0;
+// CHECK: @n1 = {{(dso_local )?}}global i8 0
``````````
</details>
https://github.com/llvm/llvm-project/pull/224873
More information about the cfe-commits
mailing list