Skip to content

[BUG] Generated union copy constructor can be invalid #772

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

Closed
KendallHarter opened this issue Oct 23, 2023 · 4 comments
Closed

[BUG] Generated union copy constructor can be invalid #772

KendallHarter opened this issue Oct 23, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@KendallHarter
Copy link

Describe the bug
Copying a union can result in invalid pointers errors.

To Reproduce
Sample code:

test: @union type = {
   name: std::string;
}

main: () = {
   a1: test = ();
   a1.set_name("hi");
   a2 := a1;
}

Commands:

./cppfront test.cpp2
g++11.3 -I ./include/ -std=c++20 test.cpp
./a.out

Expected result:
Program runs without error.

Actual result/error:

munmap_chunk(): invalid pointer
Aborted

Additional context
This seems to be caused by the implicit member-wise copying having no opt-out option, as it'll copy the storage before potentially deleting it with the set_name function.

@KendallHarter KendallHarter added the bug Something isn't working label Oct 23, 2023
@JohelEGP
Copy link
Contributor

Yes.
It copies the storage and discriminator.
So it believes it has a live object when the set_* first calls destroy().

@JohelEGP
Copy link
Contributor

Smells like #475.

@JohelEGP
Copy link
Contributor

JohelEGP commented Oct 23, 2023

The general operator= should default-initialize the storage and set the discriminator to -1.
Better yet, overload operator=: (inout this, that); to not assign {} to the storage.

@JohelEGP
Copy link
Contributor

JohelEGP commented Nov 8, 2023

Resolved by commit cdf71bd.

@hsutter hsutter closed this as completed Nov 8, 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
Projects
None yet
Development

No branches or pull requests

3 participants