Skip to content

Commit 7bc793a

Browse files
authored
[clang][Interp] Check pointer inc/dec ops for null (#69168)
1 parent cc6a5ea commit 7bc793a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,11 +1488,14 @@ static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC,
14881488
const Pointer &Ptr) {
14891489
using OneT = Integral<8, false>;
14901490

1491+
const Pointer &P = Ptr.deref<Pointer>();
1492+
if (!CheckNull(S, OpPC, P, CSK_ArrayIndex))
1493+
return false;
1494+
14911495
// Get the current value on the stack.
1492-
S.Stk.push<Pointer>(Ptr.deref<Pointer>());
1496+
S.Stk.push<Pointer>(P);
14931497

14941498
// Now the current Ptr again and a constant 1.
1495-
Pointer P = Ptr.deref<Pointer>();
14961499
OneT One = OneT::from(1);
14971500
if (!OffsetHelper<OneT, Op>(S, OpPC, One, P))
14981501
return false;

clang/test/AST/Interp/arrays.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,26 @@ namespace IncDec {
333333
// expected-note {{in call to}} \
334334
// ref-error {{not an integral constant expression}} \
335335
// ref-note {{in call to}}
336+
337+
constexpr int nullptr1(bool Pre) {
338+
int *a = nullptr;
339+
if (Pre)
340+
++a; // ref-note {{arithmetic on null pointer}} \
341+
// expected-note {{arithmetic on null pointer}}
342+
else
343+
a++; // ref-note {{arithmetic on null pointer}} \
344+
// expected-note {{arithmetic on null pointer}}
345+
return 1;
346+
}
347+
static_assert(nullptr1(true) == 1, ""); // ref-error {{not an integral constant expression}} \
348+
// ref-note {{in call to}} \
349+
// expected-error {{not an integral constant expression}} \
350+
// expected-note {{in call to}}
351+
352+
static_assert(nullptr1(false) == 1, ""); // ref-error {{not an integral constant expression}} \
353+
// ref-note {{in call to}} \
354+
// expected-error {{not an integral constant expression}} \
355+
// expected-note {{in call to}}
336356
};
337357

338358
namespace ZeroInit {

0 commit comments

Comments
 (0)