Skip to content

[clang][bytecode] Clear inactive union fields when copying #134982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
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
44 changes: 43 additions & 1 deletion clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2723,6 +2723,45 @@ bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC,
return true;
}

static void zeroAll(Pointer &Dest) {
const Descriptor *Desc = Dest.getFieldDesc();

if (Desc->isPrimitive()) {
TYPE_SWITCH(Desc->getPrimType(), {
Dest.deref<T>().~T();
new (&Dest.deref<T>()) T();
});
return;
}

if (Desc->isRecord()) {
const Record *R = Desc->ElemRecord;
for (const Record::Field &F : R->fields()) {
Pointer FieldPtr = Dest.atField(F.Offset);
zeroAll(FieldPtr);
}
return;
}

if (Desc->isPrimitiveArray()) {
for (unsigned I = 0, N = Desc->getNumElems(); I != N; ++I) {
TYPE_SWITCH(Desc->getPrimType(), {
Dest.deref<T>().~T();
new (&Dest.deref<T>()) T();
});
}
return;
}

if (Desc->isCompositeArray()) {
for (unsigned I = 0, N = Desc->getNumElems(); I != N; ++I) {
Pointer ElemPtr = Dest.atIndex(I).narrow();
zeroAll(ElemPtr);
}
return;
}
}

static bool copyComposite(InterpState &S, CodePtr OpPC, const Pointer &Src,
Pointer &Dest, bool Activate);
static bool copyRecord(InterpState &S, CodePtr OpPC, const Pointer &Src,
Expand Down Expand Up @@ -2751,11 +2790,14 @@ static bool copyRecord(InterpState &S, CodePtr OpPC, const Pointer &Src,
const Record *R = DestDesc->ElemRecord;
for (const Record::Field &F : R->fields()) {
if (R->isUnion()) {
// For unions, only copy the active field.
// For unions, only copy the active field. Zero all others.
const Pointer &SrcField = Src.atField(F.Offset);
if (SrcField.isActive()) {
if (!copyField(F, /*Activate=*/true))
return false;
} else {
Pointer DestField = Dest.atField(F.Offset);
zeroAll(DestField);
}
} else {
if (!copyField(F, Activate))
Expand Down
Loading