Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,9 @@ bool InitField(InterpState &S, CodePtr OpPC, uint32_t I) {
const Pointer &Ptr = S.Stk.peek<Pointer>();
if (!CheckRange(S, OpPC, Ptr, CSK_Field))
return false;
if (!CheckArray(S, OpPC, Ptr))
return false;

const Pointer &Field = Ptr.atField(I);
Field.deref<T>() = Value;
Field.initialize();
Expand All @@ -1652,6 +1655,9 @@ bool InitFieldActivate(InterpState &S, CodePtr OpPC, uint32_t I) {
const Pointer &Ptr = S.Stk.peek<Pointer>();
if (!CheckRange(S, OpPC, Ptr, CSK_Field))
return false;
if (!CheckArray(S, OpPC, Ptr))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like CheckArray is really DiagnoseIfArrayOfUnknownSize a mouthful for sure.

return false;

const Pointer &Field = Ptr.atField(I);
Field.deref<T>() = Value;
Field.activate();
Expand All @@ -1663,7 +1669,13 @@ template <PrimType Name, class T = typename PrimConv<Name>::T>
bool InitBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
assert(F->isBitField());
const T &Value = S.Stk.pop<T>();
const Pointer &Field = S.Stk.peek<Pointer>().atField(F->Offset);
const Pointer &Ptr = S.Stk.peek<Pointer>();
if (!CheckRange(S, OpPC, Ptr, CSK_Field))
return false;
if (!CheckArray(S, OpPC, Ptr))
return false;

const Pointer &Field = Ptr.atField(F->Offset);

if constexpr (needsAlloc<T>()) {
T Result = S.allocAP<T>(Value.bitWidth());
Expand All @@ -1689,7 +1701,13 @@ bool InitBitFieldActivate(InterpState &S, CodePtr OpPC,
const Record::Field *F) {
assert(F->isBitField());
const T &Value = S.Stk.pop<T>();
const Pointer &Field = S.Stk.peek<Pointer>().atField(F->Offset);
const Pointer &Ptr = S.Stk.peek<Pointer>();
if (!CheckRange(S, OpPC, Ptr, CSK_Field))
return false;
if (!CheckArray(S, OpPC, Ptr))
return false;

const Pointer &Field = Ptr.atField(F->Offset);

if constexpr (needsAlloc<T>()) {
T Result = S.allocAP<T>(Value.bitWidth());
Expand Down
20 changes: 20 additions & 0 deletions clang/test/AST/ByteCode/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,23 @@ namespace ZeroSizeArrayRead {
static_assert(s[0] == '0', ""); // both-error {{not an integral constant expression}} \
// both-note {{read of dereferenced one-past-the-end pointer}}
}

namespace FAM {
char *strchr(const char *, int);

struct A {
char n, a[2];
};
struct B {
int n;
struct A a[]; // both-note {{here}}
};

const struct B b = {0, {{1, {2, 3}}, {4, {5, 6}}}};
void foo(void) { int sch = 0 != strchr(b.a[1].a, '\0'); }

int foo2() {
struct B b = {0, {{1, {2, 3}}, {4, {5, 6}}}}; // both-error {{initialization of flexible array member is not allowed}}
return 1;
}
}