[clang] 9817e0b - [clang][bytecode] Diagnose one-past-end pointers in evaluateStringRepr (#237882)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 2 02:47:27 PST 2026


Author: Timm Baeder
Date: 2026-11-02T11:47:22+01:00
New Revision: 9817e0b07b3a0e49556f0b4cc38e613093a99980

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

LOG: [clang][bytecode] Diagnose one-past-end pointers in evaluateStringRepr (#237882)

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Context.cpp
    clang/test/SemaCXX/static-assert-cxx26.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index 5dd73467dfe914..378a1f6f6538b1 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -219,8 +219,14 @@ bool Context::evaluateStringRepr(const EvalSettings &Settings,
       return false;
     }
 
-    if (!Ptr.isLive() || !Ptr.isInitialized() || Ptr.isUnknownSizeArray() ||
-        !Ptr.inArray())
+    if (Ptr.isOnePastEnd()) {
+      S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_past_end)
+          << AK_Read;
+      return false;
+    }
+
+    if (!Ptr.isLive() || Ptr.isUnknownSizeArray() || !Ptr.inArray() ||
+        !Ptr.isReadablePointerType() || !Ptr.isInitialized())
       return false;
 
     // Must be char.

diff  --git a/clang/test/SemaCXX/static-assert-cxx26.cpp b/clang/test/SemaCXX/static-assert-cxx26.cpp
index 644805a9e84c7d..043f9dbf189323 100644
--- a/clang/test/SemaCXX/static-assert-cxx26.cpp
+++ b/clang/test/SemaCXX/static-assert-cxx26.cpp
@@ -426,3 +426,15 @@ namespace DataPtrIsNull {
                              // expected-note {{read of dereferenced null pointer is not allowed in a constant expression}} \
                              // expected-error {{static assertion failed}}
 }
+
+namespace OnePastEndRead {
+  constexpr const char foo[] = {};
+
+  struct S {
+    constexpr int size() const { return 4; }
+    constexpr const char *data() const { return foo; }
+  };
+  static_assert(false, S{}); // expected-error {{must be produced by a constant expression}} \
+                             // expected-note {{read of dereferenced one-past-the-end pointer}} \
+                             // expected-error {{static assertion failed}}
+}


        


More information about the cfe-commits mailing list