[clang] [clang][bytecode] Fix base cast of nullptr without descriptor (PR #132909)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 25 03:17:06 PDT 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/132909

The missing descriptor should only happen if the pointer is null pointer.

>From ac98de068f55909a1b8bdd3070de561cd5d913e2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 25 Mar 2025 11:15:52 +0100
Subject: [PATCH] [clang][bytecode] Fix base cast of nullptr without descriptor

The missing descriptor should only happen if the pointer is null
pointer.
---
 clang/lib/AST/ByteCode/Pointer.cpp  |  4 ++++
 clang/test/AST/ByteCode/records.cpp | 16 ++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 8abdc54b64677..79b47c26992ae 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -720,6 +720,10 @@ IntPointer IntPointer::atOffset(const ASTContext &ASTCtx,
 
 IntPointer IntPointer::baseCast(const ASTContext &ASTCtx,
                                 unsigned BaseOffset) const {
+  if (!Desc) {
+    assert(Value == 0);
+    return *this;
+  }
   const Record *R = Desc->ElemRecord;
   const Descriptor *BaseDesc = nullptr;
 
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 5d9f1044f206b..da851785323a5 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1771,3 +1771,19 @@ namespace RedeclaredCtor {
   constexpr __sp_mut::__sp_mut(void *p) noexcept : __lx_(p) {}
   constexpr __sp_mut muts = &mut_back[0];
 }
+
+namespace IntegralBaseCast {
+  class A {};
+  class B : public A {};
+  struct S {
+    B *a;
+  };
+
+  constexpr int f() {
+    S s{};
+    A *a = s.a;
+    return 0;
+  }
+
+  static_assert(f() == 0, "");
+}



More information about the cfe-commits mailing list