Skip to content
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

Possibly undefined delete due to unsafe use of unique_ptr #96980

Open
leni536 opened this issue Jun 27, 2024 · 0 comments
Open

Possibly undefined delete due to unsafe use of unique_ptr #96980

leni536 opened this issue Jun 27, 2024 · 0 comments

Comments

@leni536
Copy link

leni536 commented Jun 27, 2024

std::unique_ptr<ConstantDataSequential> *Entry = &Slot.second;
for (; *Entry; Entry = &(*Entry)->Next)
if ((*Entry)->getType() == Ty)
return Entry->get();
// Okay, we didn't get a hit. Create a node of the right class, link it in,
// and return it.
if (isa<ArrayType>(Ty)) {
// Use reset because std::make_unique can't access the constructor.
Entry->reset(new ConstantDataArray(Ty, Slot.first().data()));
return Entry->get();
}
assert(isa<VectorType>(Ty));
// Use reset because std::make_unique can't access the constructor.
Entry->reset(new ConstantDataVector(Ty, Slot.first().data()));
return Entry->get();

ConstantDataSequential is a non-polymorphic base class of ConstantDataArray and ConstantDataVector.
*Entry is unique_ptr<ConstantDataSequential> and it gets to own either a dynamically allocated ConstantDataArray or ConstantDataVector, but at the end of its lifetime it deletes the owned object through a pointer of ConstantDataSequential. As ConstantDataSequential has no virtual destructor, this is undefined behavior.

The undefined behavior might manifest benignly or less benignly depending whether sized deallocation functions are enabled.

The bug was found by a reference implementation of https://wg21.link/P2413R1 .

@leni536 leni536 changed the title Possibly undefined delete due to unsafe conversion of unique_ptr Possibly undefined delete due to unsafe use of unique_ptr Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants