Skip to content

Commit

Permalink
[clang][Interp] Protect Inc/Dec ops against dummy pointers
Browse files Browse the repository at this point in the history
We create them more often in C, so it's more likely to happen there.
  • Loading branch information
tbaederr committed Feb 1, 2024
1 parent 021a2b4 commit a9e8309
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,9 @@ enum class IncDecOp {

template <typename T, IncDecOp Op, PushVal DoPush>
bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (Ptr.isDummy())
return false;

const T &Value = Ptr.deref<T>();
T Result;

Expand Down Expand Up @@ -1521,6 +1524,9 @@ bool SubOffset(InterpState &S, CodePtr OpPC) {
template <ArithOp Op>
static inline bool IncDecPtrHelper(InterpState &S, CodePtr OpPC,
const Pointer &Ptr) {
if (Ptr.isDummy())
return false;

using OneT = Integral<8, false>;

const Pointer &P = Ptr.deref<Pointer>();
Expand Down
10 changes: 10 additions & 0 deletions clang/test/AST/Interp/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ const intptr_t L = (intptr_t)(&(yy->y)); // expected-error {{not a compile-time
const ptrdiff_t m = &m + 137 - &m;
_Static_assert(m == 137, ""); // pedantic-ref-warning {{GNU extension}} \
// pedantic-expected-warning {{GNU extension}}

/// from test/Sema/switch.c, used to cause an assertion failure.
void f (int z) {
while (z) {
default: z--; // expected-error {{'default' statement not in switch}} \
// pedantic-expected-error {{'default' statement not in switch}} \
// ref-error {{'default' statement not in switch}} \
// pedantic-ref-error {{'default' statement not in switch}}
}
}
1 change: 1 addition & 0 deletions clang/test/Sema/check-increment.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// RUN: %clang_cc1 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
// expected-no-diagnostics

int printf(const char *, ...);
Expand Down

0 comments on commit a9e8309

Please sign in to comment.