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

Dangling pointer after move/copy constructor of dynamic bit field arrays #501

Closed
mikir opened this issue May 5, 2023 · 0 comments
Closed
Assignees
Labels
bug Something isn't working c++ C++ language generator
Milestone

Comments

@mikir
Copy link
Contributor

mikir commented May 5, 2023

If structure which contains dynamic bit field array is moved or copied, the moved or copied structure still keeps reference to the original structure. This can easily lead to undefined behavior as soon as the original structure is release.

Consider the following schema:

struct DynamicBitFieldArrayStruct
{
    uint8 bitLen;
    bit<bitLen> dynamicBitFieldArray[];
};

and the following test application

int main(int argc, char* argv[])
{
    DynamicBitFieldArrayStruct copy;
    {
        DynamicBitFieldArrayStruct orig(5, {{1, 2, 3, 4, 5 }});
        auto bitBuffer = zserio::serialize(orig);
        std::cout << "orig:" << orig.bitSizeOf() << std::endl;

        copy = orig;

        std::cout << "copy 1:" << orig.bitSizeOf() << std::endl;
    }

    // this copy keeps reference to orig but orig is still in the memory
    std::cout << "copy 2:" << copy.bitSizeOf() << std::endl;

    // overwrite orig
    std::array<uint8_t, 7> arr;
    arr.fill(0xFF);

    // error!
    std::cout << "copy 3:" << copy.bitSizeOf() << std::endl;

    return;
}

Then, the following output can be observed:

orig:41
copy 1:41
copy 2:41
copy 3:1291
@mikir mikir added bug Something isn't working c++ C++ language generator labels May 5, 2023
@mikir mikir added this to the 2.11 milestone May 5, 2023
Mi-La pushed a commit that referenced this issue May 9, 2023
Re-initialization is needed after copy/move operations.
Mi-La added a commit that referenced this issue May 10, 2023
Does not work with cross compilation.
Mi-La pushed a commit that referenced this issue May 10, 2023
Re-initialization is needed after copy/move operations.
@mikir mikir closed this as completed May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working c++ C++ language generator
Projects
None yet
Development

No branches or pull requests

2 participants