Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

UB: QirTupleHeader::Release() #565

@kuzminrobin

Description

@kuzminrobin

The instruction return this->refCount; can access the dangling pointer.

int QirTupleHeader::Release()
{
    . . .
    if (this->refCount == 0)
    {
        char* buffer = reinterpret_cast<char*>(this);
        delete[] buffer;   // Here the data pointed to by `this` gets deallocated. The `this` becomes a dangling pointer.
    }
    return this->refCount;   // This has a risk of accessing the dangling pointer. Undefined Behavior.
}

Consider changing to

int QirTupleHeader::Release()
{
    . . .
    int32_t retVal = this->refCount;   // Memorize in the local variable the `refCount`.
    if (this->refCount == 0)
    {
        char* buffer = reinterpret_cast<char*>(this);
        delete[] buffer;   // Here the data pointed to by `this` is deallocated. The `this` becomes a dangling pointer.
    }
    return retVal;   // Access the local variable.
}

Consider also syncing the data type returned by int QirTupleHeader::Release() and type of this->refCount - int32_t.

Metadata

Metadata

Assignees

Labels

area: QIRQIR runtimebugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions