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

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 30 00:32:43 PDT 2026


Author: Timm Baeder
Date: 2026-04-30T09:32:38+02:00
New Revision: b8551d94d4ce33cf16dbbe3f4f512e6ee2dce899

URL: https://github.com/llvm/llvm-project/commit/b8551d94d4ce33cf16dbbe3f4f512e6ee2dce899
DIFF: https://github.com/llvm/llvm-project/commit/b8551d94d4ce33cf16dbbe3f4f512e6ee2dce899.diff

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

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

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.h
    clang/test/AST/ByteCode/addr-label-diff.cpp

Removed: 
    


################################################################################
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