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

Add repr(C) to unions to ensure variants are laid out at the start #74

Merged
merged 1 commit into from
Jan 16, 2022

Conversation

5225225
Copy link
Contributor

@5225225 5225225 commented Jan 16, 2022

I found this bug when investigating use cases for the lint proposed rust-lang/rust-clippy#8289 (comment)

If a union doesn't have a #[repr(C)] attribute on it, the two variants do not have to be laid out both starting at offset 0. It would be perfectly valid to treat that union keyword as a struct keyword, and then suddenly you're reading uninit data / getting the wrong results.

Source for this: https://doc.rust-lang.org/reference/types/union.html says

The memory layout of a union is undefined by default, but the #[repr(...)] attribute can be used to fix a layout.

@kornelski
Copy link
Member

Great catch! Thank you

@kornelski kornelski merged commit 69537bf into ImageOptim:main Jan 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants