Skip to content

Commit 026fbdf

Browse files
committed
[clang][Interp] Handle one-past-the-end pointers in SubPtr
1 parent 86295dc commit 026fbdf

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,8 +1773,10 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC) {
17731773
return true;
17741774
}
17751775

1776-
T A = T::from(LHS.getIndex());
1777-
T B = T::from(RHS.getIndex());
1776+
T A = LHS.isElementPastEnd() ? T::from(LHS.getNumElems())
1777+
: T::from(LHS.getIndex());
1778+
T B = RHS.isElementPastEnd() ? T::from(RHS.getNumElems())
1779+
: T::from(RHS.getIndex());
17781780
return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, A.bitWidth(), A, B);
17791781
}
17801782

clang/test/AST/Interp/arrays.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,9 @@ namespace ArrayMemberAccess {
609609
bool cond = a->x;
610610
}
611611
}
612+
613+
namespace OnePastEndSub {
614+
struct A {};
615+
constexpr A a[3][3];
616+
constexpr int diff2 = &a[1][3] - &a[1][0]; /// Used to crash.
617+
}

0 commit comments

Comments
 (0)