Skip to content

Commit 12d4903

Browse files
tbaederrSterling-Augustine
authored andcommitted
[clang][bytecode] Check GetPtrBase ops for null pointers (llvm#110673)
1 parent bef3cc2 commit 12d4903

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,14 +1641,14 @@ inline bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off) {
16411641

16421642
inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
16431643
const Pointer &Ptr = S.Stk.peek<Pointer>();
1644+
if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1645+
return false;
16441646

16451647
if (!Ptr.isBlockPointer()) {
16461648
S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
16471649
return true;
16481650
}
16491651

1650-
if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1651-
return false;
16521652
if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
16531653
return false;
16541654
const Pointer &Result = Ptr.atField(Off);
@@ -1661,13 +1661,14 @@ inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
16611661
inline bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off) {
16621662
const Pointer &Ptr = S.Stk.pop<Pointer>();
16631663

1664+
if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1665+
return false;
1666+
16641667
if (!Ptr.isBlockPointer()) {
16651668
S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
16661669
return true;
16671670
}
16681671

1669-
if (!CheckNull(S, OpPC, Ptr, CSK_Base))
1670-
return false;
16711672
if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
16721673
return false;
16731674
const Pointer &Result = Ptr.atField(Off);

clang/test/AST/ByteCode/records.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,3 +1653,11 @@ namespace ExprWithCleanups {
16531653
constexpr auto F = true ? 1i : 2i;
16541654
static_assert(F == 1i, "");
16551655
}
1656+
1657+
namespace NullptrUpcast {
1658+
struct A {};
1659+
struct B : A { int n; };
1660+
constexpr B *nb = nullptr;
1661+
constexpr A &ra = *nb; // both-error {{constant expression}} \
1662+
// both-note {{cannot access base class of null pointer}}
1663+
}

0 commit comments

Comments
 (0)