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

unsound core::mem::zeroed() #29

Open
Dirbaio opened this issue Sep 11, 2024 · 1 comment
Open

unsound core::mem::zeroed() #29

Dirbaio opened this issue Sep 11, 2024 · 1 comment

Comments

@Dirbaio
Copy link

Dirbaio commented Sep 11, 2024

let timer = unsafe { core::mem::zeroed() };

This line is unsound. Creating an invalid timer is UB itself, even if the value is never used. The timer could contain types which the zero valued is invalid, such as &T or NonZeroU32.

See https://doc.rust-lang.org/reference/behavior-considered-undefined.html

  • Producing an invalid value, even in private fields and locals. “Producing” a value happens any time a value is assigned to or read from a place, passed to a function/primitive operation or returned from a function/primitive operation.

Possible solutions:

  • fn timer_mut(&mut self) -> &mut Timer, but then the caller can't reconstruct the timer.
  • store a Option<Timer> in Self.
@sajattack
Copy link
Owner

sajattack commented Sep 14, 2024

Option<Timer> seems like a good choice.

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

No branches or pull requests

2 participants