Skip to content

Commit

Permalink
[clang][Interp] Allow stepping back from a one-past-the-end pointer
Browse files Browse the repository at this point in the history
... back into range of the array.
  • Loading branch information
tbaederr committed May 22, 2024
1 parent c912f0e commit 9604e5c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
6 changes: 5 additions & 1 deletion clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1555,8 +1555,12 @@ bool OffsetHelper(InterpState &S, CodePtr OpPC, const T &Offset,
if (!CheckArray(S, OpPC, Ptr))
return false;

uint64_t Index = Ptr.getIndex();
uint64_t MaxIndex = static_cast<uint64_t>(Ptr.getNumElems());
uint64_t Index;
if (Ptr.isOnePastEnd())
Index = MaxIndex;
else
Index = Ptr.getIndex();

bool Invalid = false;
// Helper to report an invalid offset, computed as APSInt.
Expand Down
3 changes: 0 additions & 3 deletions clang/lib/AST/Interp/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,6 @@ class Pointer {
if (isZero())
return 0;

if (isElementPastEnd())
return 1;

// narrow()ed element in a composite array.
if (asBlockPointer().Base > sizeof(InlineDescriptor) &&
asBlockPointer().Base == Offset)
Expand Down
4 changes: 4 additions & 0 deletions clang/test/AST/Interp/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ constexpr int derefPtr(const int *d) {
}
static_assert(derefPtr(data) == 5, "");

/// Make sure we can refer to the one-past-the-end element
/// and then return back to the end of the array.
static_assert((&data[5])[-1] == 1, "");

constexpr int storePtr() {
int b[] = {1,2,3,4};
int *c = b;
Expand Down

0 comments on commit 9604e5c

Please sign in to comment.