[clang] [clang][bytecode] Fix null ptr upcasts adding offset (PR #224873)
Benji Smith via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 19 17:13:34 PDT 2026
https://github.com/Benjins created https://github.com/llvm/llvm-project/pull/224873
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
>From 940c0518978692b0a339bc909264b0e93f411f62 Mon Sep 17 00:00:00 2001
From: Benji Smith <benjsith at gmail.com>
Date: Sat, 19 Sep 2026 19:17:18 -0400
Subject: [PATCH] [clang][bytecode] Fix null ptr upcasts adding offset
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
---
clang/docs/ReleaseNotes.md | 3 +++
clang/lib/AST/ByteCode/Pointer.cpp | 4 ++++
clang/test/AST/ByteCode/const-base-cast.cpp | 5 +++++
3 files changed, 12 insertions(+)
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
More information about the cfe-commits
mailing list