Skip to content

Commit

Permalink
[clang][Interp] Protect ArrayDecay ops against dummy pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaederr committed Feb 9, 2024
1 parent df2513c commit 79e43eb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,9 @@ inline bool ArrayElemPop(InterpState &S, CodePtr OpPC, uint32_t Index) {
inline bool ArrayDecay(InterpState &S, CodePtr OpPC) {
const Pointer &Ptr = S.Stk.pop<Pointer>();

if (Ptr.isDummy())
return false;

if (!Ptr.isUnknownSizeArray()) {
S.Stk.push<Pointer>(Ptr.atIndex(0));
return true;
Expand Down
20 changes: 20 additions & 0 deletions clang/test/AST/Interp/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,23 @@ namespace NonConstReads {
const int y = 0;
int yy[y];
}

namespace SelfComparison {
struct S {
int field;
static int static_field;
int array[4];
};

struct T {
int field;
static int static_field;
int array[4];
S s;
};

int struct_test(S s1, S s2, S *s3, T t) {
return s3->array[t.field] == s3->array[t.field]; // expected-warning {{self-comparison always evaluates to true}} \
// ref-warning {{self-comparison always evaluates to true}}
};
}
1 change: 1 addition & 0 deletions clang/test/SemaCXX/self-comparison.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++2a
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++2a -fexperimental-new-constant-interpreter

int foo(int x) {
return x == x; // expected-warning {{self-comparison always evaluates to true}}
Expand Down

0 comments on commit 79e43eb

Please sign in to comment.