diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h --- a/clang/lib/AST/Interp/Interp.h +++ b/clang/lib/AST/Interp/Interp.h @@ -67,9 +67,9 @@ bool CheckRange(InterpState &S, CodePtr OpPC, const Pointer &Ptr, CheckSubobjectKind CSK); -/// Checks if accessing a base or derived record of the given pointer is valid. -bool CheckBaseDerived(InterpState &S, CodePtr OpPC, const Pointer &Ptr, - CheckSubobjectKind CSK); +/// Checks if Ptr is a one-past-the-end pointer. +bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr, + CheckSubobjectKind CSK); /// Checks if a pointer points to const storage. bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr); @@ -1121,6 +1121,9 @@ return false; if (!CheckRange(S, OpPC, Ptr, CSK_Field)) return false; + if (!CheckSubobject(S, OpPC, Ptr, CSK_Field)) + return false; + S.Stk.push(Ptr.atField(Off)); return true; } @@ -1165,7 +1168,7 @@ const Pointer &Ptr = S.Stk.pop(); if (!CheckNull(S, OpPC, Ptr, CSK_Derived)) return false; - if (!CheckBaseDerived(S, OpPC, Ptr, CSK_Derived)) + if (!CheckSubobject(S, OpPC, Ptr, CSK_Derived)) return false; S.Stk.push(Ptr.atFieldSub(Off)); return true; @@ -1175,7 +1178,7 @@ const Pointer &Ptr = S.Stk.peek(); if (!CheckNull(S, OpPC, Ptr, CSK_Base)) return false; - if (!CheckBaseDerived(S, OpPC, Ptr, CSK_Base)) + if (!CheckSubobject(S, OpPC, Ptr, CSK_Base)) return false; S.Stk.push(Ptr.atField(Off)); return true; @@ -1185,7 +1188,7 @@ const Pointer &Ptr = S.Stk.pop(); if (!CheckNull(S, OpPC, Ptr, CSK_Base)) return false; - if (!CheckBaseDerived(S, OpPC, Ptr, CSK_Base)) + if (!CheckSubobject(S, OpPC, Ptr, CSK_Base)) return false; S.Stk.push(Ptr.atField(Off)); return true; diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp --- a/clang/lib/AST/Interp/Interp.cpp +++ b/clang/lib/AST/Interp/Interp.cpp @@ -213,8 +213,8 @@ return false; } -bool CheckBaseDerived(InterpState &S, CodePtr OpPC, const Pointer &Ptr, - CheckSubobjectKind CSK) { +bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr, + CheckSubobjectKind CSK) { if (!Ptr.isOnePastEnd()) return true; diff --git a/clang/test/AST/Interp/records.cpp b/clang/test/AST/Interp/records.cpp --- a/clang/test/AST/Interp/records.cpp +++ b/clang/test/AST/Interp/records.cpp @@ -531,6 +531,29 @@ //static_assert(b.a.f == 100, ""); } +namespace PointerArith { + struct A {}; + struct B : A { int n; }; + + B b = {}; + constexpr A *a1 = &b; + constexpr B *b1 = &b + 1; + constexpr B *b2 = &b + 0; + +#if 0 + constexpr A *a2 = &b + 1; // expected-error {{must be initialized by a constant expression}} \ + // expected-note {{cannot access base class of pointer past the end of object}} \ + // ref-error {{must be initialized by a constant expression}} \ + // ref-note {{cannot access base class of pointer past the end of object}} + +#endif + constexpr const int *pn = &(&b + 1)->n; // expected-error {{must be initialized by a constant expression}} \ + // expected-note {{cannot access field of pointer past the end of object}} \ + // ref-error {{must be initialized by a constant expression}} \ + // ref-note {{cannot access field of pointer past the end of object}} + +} + #if __cplusplus >= 202002L namespace VirtualCalls { namespace Obvious {