[clang] [clang][bytecode] Fix a crash with addr label diffs (PR #195011)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 29 23:12:57 PDT 2026


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

Can't call `getPtr()` on numbers.

>From c4e67780d4614a81a0b1183103865e72fbd83646 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 30 Apr 2026 08:10:16 +0200
Subject: [PATCH] [clang][bytecode] Fix a crash with addr label diffs

Can't call `getPtr()` on numbers.
---
 clang/lib/AST/ByteCode/Interp.h             | 11 ++++++++---
 clang/test/AST/ByteCode/addr-label-diff.cpp |  2 ++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 426d26be0169c..f89ff3f18ce9d 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -418,9 +418,14 @@ bool Sub(InterpState &S, CodePtr OpPC) {
     // a AddrLabelDiff integral.
     if (LHS.getKind() == IntegralKind::LabelAddress ||
         RHS.getKind() == IntegralKind::LabelAddress) {
-      const auto *A = reinterpret_cast<const Expr *>(LHS.getPtr());
-      const auto *B = reinterpret_cast<const Expr *>(RHS.getPtr());
-      if (!isa<AddrLabelExpr>(A) || !isa<AddrLabelExpr>(B))
+      const auto *A = LHS.getKind() == IntegralKind::LabelAddress
+                          ? reinterpret_cast<const Expr *>(LHS.getPtr())
+                          : nullptr;
+      const auto *B = RHS.getKind() == IntegralKind::LabelAddress
+                          ? reinterpret_cast<const Expr *>(RHS.getPtr())
+                          : nullptr;
+      if (!isa_and_nonnull<AddrLabelExpr>(A) ||
+          !isa_and_nonnull<AddrLabelExpr>(B))
         return false;
       const auto *LHSAddrExpr = cast<AddrLabelExpr>(A);
       const auto *RHSAddrExpr = cast<AddrLabelExpr>(B);
diff --git a/clang/test/AST/ByteCode/addr-label-diff.cpp b/clang/test/AST/ByteCode/addr-label-diff.cpp
index 336a0fbe3de3e..b1049b6de1869 100644
--- a/clang/test/AST/ByteCode/addr-label-diff.cpp
+++ b/clang/test/AST/ByteCode/addr-label-diff.cpp
@@ -14,3 +14,5 @@ void UnfoldableAddrLabelDiff2() { static short x = (long)&&a-(long)&&b; a:b:retu
 // CHECK: @_ZZ21FoldableAddrLabelDiffvE1x = internal global i64 sub (i64 ptrtoint (ptr blockaddress(@_Z21FoldableAddrLabelDiffv
 void FoldableAddrLabelDiff() { static long x = (long)&&a-(long)&&b; a:b:return;}
 
+typedef decltype(sizeof(int)) L;
+void foo1() { (L) & (*(int (*)[4]) nullptr)[0] - (L) && b; b:}



More information about the cfe-commits mailing list