Skip to content

Commit 6196b4e

Browse files
authored
[clang][bytecode] Don't set OnePastEnd bit for array elements (#136422)
If we refer to arr[N], don't set the OnePastEnd bit of the APValue, since that is already encoded in the array index.
1 parent 8fdebff commit 6196b4e

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

clang/lib/AST/ByteCode/Pointer.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
223223
UsePath = false;
224224

225225
// Build the path into the object.
226+
bool OnePastEnd = isOnePastEnd();
226227
Pointer Ptr = *this;
227228
while (Ptr.isField() || Ptr.isArrayElement()) {
228229

@@ -251,9 +252,10 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
251252
Ptr = Ptr.expand();
252253
const Descriptor *Desc = Ptr.getFieldDesc();
253254
unsigned Index;
254-
if (Ptr.isOnePastEnd())
255+
if (Ptr.isOnePastEnd()) {
255256
Index = Ptr.getArray().getNumElems();
256-
else
257+
OnePastEnd = false;
258+
} else
257259
Index = Ptr.getIndex();
258260

259261
QualType ElemType = Desc->getElemQualType();
@@ -304,8 +306,7 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
304306
std::reverse(Path.begin(), Path.end());
305307

306308
if (UsePath)
307-
return APValue(Base, Offset, Path,
308-
/*IsOnePastEnd=*/!isElementPastEnd() && isOnePastEnd());
309+
return APValue(Base, Offset, Path, OnePastEnd);
309310

310311
return APValue(Base, Offset, APValue::NoLValuePath());
311312
}

clang/test/AST/ByteCode/arrays.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -761,3 +761,13 @@ namespace PointerSubscript {
761761
struct S{};
762762
static_assert((foo<S>(), true));
763763
}
764+
765+
namespace OnePastEndDiag {
766+
767+
constexpr int a(const int *b) {
768+
return *b; // both-note {{read of dereferenced one-past-the-end pointer}}
769+
}
770+
constexpr int foo[] = {1,2};
771+
constexpr int k = a(foo + 2); // both-error {{must be initialized by a constant expression}} \
772+
// both-note {{in call to 'a(&foo[2])'}}
773+
}

0 commit comments

Comments
 (0)