Skip to content

Commit 328fa47

Browse files
committed
[clang][bytecode] Handle reads on zero-size arrays
1 parent 672f82a commit 328fa47

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ bool CheckNull(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
518518

519519
bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
520520
AccessKinds AK) {
521-
if (!Ptr.isOnePastEnd())
521+
if (!Ptr.isOnePastEnd() && !Ptr.isZeroSizeArray())
522522
return true;
523523
if (S.getLangOpts().CPlusPlus) {
524524
const SourceInfo &Loc = S.Current->getSource(OpPC);
@@ -829,8 +829,6 @@ bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
829829
return false;
830830
if (!CheckExtern(S, OpPC, Ptr))
831831
return false;
832-
if (!CheckRange(S, OpPC, Ptr, AK_Read))
833-
return false;
834832
if (!CheckActive(S, OpPC, Ptr, AK_Read))
835833
return false;
836834
if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), AK_Read))

clang/test/AST/ByteCode/arrays.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,3 +779,16 @@ namespace DiscardedSubScriptExpr {
779779
return true;
780780
}
781781
}
782+
783+
namespace ZeroSizeArrayRead {
784+
constexpr char str[0] = {};
785+
constexpr unsigned checksum(const char *s) {
786+
unsigned result = 0;
787+
for (const char *p = s; *p != '\0'; ++p) { // both-note {{read of dereferenced one-past-the-end pointer}}
788+
result += *p;
789+
}
790+
return result;
791+
}
792+
constexpr unsigned C = checksum(str); // both-error {{must be initialized by a constant expression}} \
793+
// both-note {{in call to}}
794+
}

0 commit comments

Comments
 (0)