[clang] [clang][bytecode] Diagnose comparisons of unrelated zero-sized pointers (PR #140695)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue May 20 02:21:09 PDT 2025


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/140695

>From 6cfad141013ce10943335713bc6b5c2e85348f07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 20 May 2025 11:17:46 +0200
Subject: [PATCH] [clang][bytecode] Diagnose comparisons of unrelated
 zero-sized pointers

---
 clang/lib/AST/ByteCode/Interp.h   | 8 ++++++++
 clang/test/AST/ByteCode/cxx11.cpp | 7 +++++++
 2 files changed, 15 insertions(+)

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 70bbfc576925e..881a8b12c2626 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -1135,6 +1135,14 @@ inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
     }
   }
 
+  if (LHS.isUnknownSizeArray() && RHS.isUnknownSizeArray()) {
+    const SourceInfo &Loc = S.Current->getSource(OpPC);
+    S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_zero_sized)
+        << LHS.toDiagnosticString(S.getASTContext())
+        << RHS.toDiagnosticString(S.getASTContext());
+    return false;
+  }
+
   S.Stk.push<BoolT>(BoolT::from(Fn(ComparisonCategoryResult::Unordered)));
   return true;
 }
diff --git a/clang/test/AST/ByteCode/cxx11.cpp b/clang/test/AST/ByteCode/cxx11.cpp
index 2a1bda675075c..368221106f640 100644
--- a/clang/test/AST/ByteCode/cxx11.cpp
+++ b/clang/test/AST/ByteCode/cxx11.cpp
@@ -243,3 +243,10 @@ namespace Volatile {
                   // both-note {{in call to 'f(0)'}}
   };
 }
+
+namespace ZeroSizeCmp {
+  extern void (*start[])();
+  extern void (*end[])();
+  static_assert(&start != &end, ""); // both-error {{constant expression}} \
+                                     // both-note {{comparison of pointers '&start' and '&end' to unrelated zero-sized objects}}
+}



More information about the cfe-commits mailing list