Skip to content

Commit

Permalink
[clang][Interp] Handle one-past-the-end pointers in SubPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaederr committed Jun 6, 2024
1 parent 86295dc commit 026fbdf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1773,8 +1773,10 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC) {
return true;
}

T A = T::from(LHS.getIndex());
T B = T::from(RHS.getIndex());
T A = LHS.isElementPastEnd() ? T::from(LHS.getNumElems())
: T::from(LHS.getIndex());
T B = RHS.isElementPastEnd() ? T::from(RHS.getNumElems())
: T::from(RHS.getIndex());
return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, A.bitWidth(), A, B);
}

Expand Down
6 changes: 6 additions & 0 deletions clang/test/AST/Interp/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,9 @@ namespace ArrayMemberAccess {
bool cond = a->x;
}
}

namespace OnePastEndSub {
struct A {};
constexpr A a[3][3];
constexpr int diff2 = &a[1][3] - &a[1][0]; /// Used to crash.
}

0 comments on commit 026fbdf

Please sign in to comment.