[clang] 6de092d - [clang][bytecode] Fix crash involving labels and null sub (#194115)

via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 25 11:04:08 PDT 2026


Author: Timm Baeder
Date: 2026-04-25T20:04:03+02:00
New Revision: 6de092dca37864cd70b55bfeaba1ec24c4382a60

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

LOG: [clang][bytecode] Fix crash involving labels and null sub (#194115)

For null pointers, getDeclDesc() may return null, so we can't call
asExpr() on it.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.h
    clang/lib/AST/ByteCode/Pointer.h
    clang/test/AST/ByteCode/c.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 63333f185bed9..293ef3c2a639c 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2611,12 +2611,15 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC, bool ElemSizeIsZero) {
 
   if (LHS.pointsToLabel() || RHS.pointsToLabel()) {
     if constexpr (isIntegralOrPointer<T>()) {
-      const auto *LHSAddrExpr =
-          dyn_cast_if_present<AddrLabelExpr>(LHS.getDeclDesc()->asExpr());
-      const auto *RHSAddrExpr =
-          dyn_cast_if_present<AddrLabelExpr>(RHS.getDeclDesc()->asExpr());
-      if (!LHSAddrExpr || !RHSAddrExpr)
+      const AddrLabelExpr *LHSAddrExpr = LHS.getPointedToLabel();
+      const AddrLabelExpr *RHSAddrExpr = RHS.getPointedToLabel();
+      if (!LHSAddrExpr || !RHSAddrExpr) {
+        S.FFDiag(S.Current->getSource(OpPC),
+                 diag::note_constexpr_pointer_arith_unspecified)
+            << LHS.toDiagnosticString(S.getASTContext())
+            << RHS.toDiagnosticString(S.getASTContext());
         return false;
+      }
 
       if (LHSAddrExpr->getLabel()->getDeclContext() !=
           RHSAddrExpr->getLabel()->getDeclContext())

diff  --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index 9cc7af4c78241..3eb9cfc4e53db 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -830,6 +830,12 @@ class Pointer {
   bool pointsToStringLiteral() const;
   /// Whether this points to a block created for an AddrLabelExpr.
   bool pointsToLabel() const;
+  /// Returns the AddrLabelExpr the Pointer points to, if any.
+  const AddrLabelExpr *getPointedToLabel() const {
+    if (const Descriptor *Desc = getDeclDesc())
+      return dyn_cast_if_present<AddrLabelExpr>(Desc->asExpr());
+    return nullptr;
+  }
 
   /// Prints the pointer.
   void print(llvm::raw_ostream &OS) const;

diff  --git a/clang/test/AST/ByteCode/c.c b/clang/test/AST/ByteCode/c.c
index 31368d0d9d62f..2e5c003ae3f0f 100644
--- a/clang/test/AST/ByteCode/c.c
+++ b/clang/test/AST/ByteCode/c.c
@@ -451,3 +451,7 @@ int *iptr;
 void ignoredConditional(void) { *iptr = (((_Complex double)1.0 ? 2 : 3), a); } // all-warning {{left operand of comma operator has no effect}}
 
 float r  = (float) (intptr_t) &r; // all-error {{initializer element is not a compile-time constant}}
+
+void labelAndNull(void) { int bar = &*(void *)0 - &&baz; } // all-error {{use of undeclared label 'baz'}} \\
+                                                           // pedantic-warning {{use of GNU address-of-label extension}} \
+                                                           // pedantic-warning {{arithmetic on pointers to void is a GNU extension}}


        


More information about the cfe-commits mailing list