diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp index bc14bd3d1bb99..b5c044cad60f8 100644 --- a/clang/lib/AST/ByteCode/Interp.cpp +++ b/clang/lib/AST/ByteCode/Interp.cpp @@ -518,7 +518,7 @@ bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr, bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, AccessKinds AK) { - if (!Ptr.isOnePastEnd()) + if (!Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray()) return true; if (S.getLangOpts().CPlusPlus) { const SourceInfo &Loc = S.Current->getSource(OpPC); @@ -829,8 +829,6 @@ bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) { return false; if (!CheckExtern(S, OpPC, Ptr)) return false; - if (!CheckRange(S, OpPC, Ptr, AK_Read)) - return false; if (!CheckActive(S, OpPC, Ptr, AK_Read)) return false; if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), AK_Read)) diff --git a/clang/test/AST/ByteCode/arrays.cpp b/clang/test/AST/ByteCode/arrays.cpp index 2dd51c2fa6711..8ef5e4d8346d4 100644 --- a/clang/test/AST/ByteCode/arrays.cpp +++ b/clang/test/AST/ByteCode/arrays.cpp @@ -779,3 +779,20 @@ namespace DiscardedSubScriptExpr { return true; } } + +namespace ZeroSizeArrayRead { + constexpr char str[0] = {}; + constexpr unsigned checksum(const char *s) { + unsigned result = 0; + for (const char *p = s; *p != '\0'; ++p) { // both-note {{read of dereferenced one-past-the-end pointer}} + result += *p; + } + return result; + } + constexpr unsigned C = checksum(str); // both-error {{must be initialized by a constant expression}} \ + // both-note {{in call to}} + + constexpr const char *p1 = &str[0]; + constexpr const char *p2 = &str[1]; // both-error {{must be initialized by a constant expression}} \ + // both-note {{cannot refer to element 1 of array of 0 elements in a constant expression}} +}