Skip to content

Commit c15e583

Browse files
authored
[clang][Interp] Fix nullptr array dereferencing (#75798)
The attached test case would cause an assertion failure in Pointer.h when operating on a null pointer.
1 parent 7e54ae2 commit c15e583

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

clang/lib/AST/Interp/Interp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ bool CheckInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
290290
}
291291

292292
bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
293-
if (!CheckDummy(S, OpPC, Ptr))
294-
return false;
295293
if (!CheckLive(S, OpPC, Ptr, AK_Read))
296294
return false;
295+
if (!CheckDummy(S, OpPC, Ptr))
296+
return false;
297297
if (!CheckExtern(S, OpPC, Ptr))
298298
return false;
299299
if (!CheckRange(S, OpPC, Ptr, AK_Read))

clang/lib/AST/Interp/Interp.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,9 +1813,6 @@ inline bool ArrayElemPtr(InterpState &S, CodePtr OpPC) {
18131813
const T &Offset = S.Stk.pop<T>();
18141814
const Pointer &Ptr = S.Stk.peek<Pointer>();
18151815

1816-
if (!CheckArray(S, OpPC, Ptr))
1817-
return false;
1818-
18191816
if (!OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr))
18201817
return false;
18211818

@@ -1843,9 +1840,6 @@ inline bool ArrayElemPtrPop(InterpState &S, CodePtr OpPC) {
18431840
const T &Offset = S.Stk.pop<T>();
18441841
const Pointer &Ptr = S.Stk.pop<Pointer>();
18451842

1846-
if (!CheckArray(S, OpPC, Ptr))
1847-
return false;
1848-
18491843
if (!OffsetHelper<T, ArithOp::Add>(S, OpPC, Offset, Ptr))
18501844
return false;
18511845

clang/test/AST/Interp/arrays.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ constexpr int getElementFromEnd(const int *Arr, int size, int index) {
7272
static_assert(getElementFromEnd(data, 5, 0) == 1, "");
7373
static_assert(getElementFromEnd(data, 5, 4) == 5, "");
7474

75+
constexpr int getFirstElem(const int *a) {
76+
return a[0]; // expected-note {{read of dereferenced null pointer}} \
77+
// ref-note {{read of dereferenced null pointer}}
78+
}
79+
static_assert(getFirstElem(nullptr) == 1, ""); // expected-error {{not an integral constant expression}} \
80+
// expected-note {{in call to}} \
81+
// ref-error {{not an integral constant expression}} \
82+
// ref-note {{in call to}}
7583

7684
constexpr static int arr[2] = {1,2};
7785
constexpr static int arr2[2] = {3,4};

0 commit comments

Comments
 (0)