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

Reading enums in Rust can lead to UB in case of incorrect file content #170

Closed
VeaaC opened this issue Apr 14, 2020 · 0 comments · Fixed by #171
Closed

Reading enums in Rust can lead to UB in case of incorrect file content #170

VeaaC opened this issue Apr 14, 2020 · 0 comments · Fixed by #171
Assignees
Labels
bug Something isn't working rust

Comments

@VeaaC
Copy link
Collaborator

VeaaC commented Apr 14, 2020

Unlike C/C++ Rust enums are exhaustive: All possible values need to be explicitly mentioned.

That means that casting from the underlying type (e.g. 9 bit integer) to the enumeration can cause UB if the value stored in flatdata does not exist in the enumeration (e.g. corrupted file, etc).

Rust doesn't currently support C-like enums. This means that our options are a bit limited. Some ideas:

  • Create enum with an additional (hidden?) field Unknown, and check in reader (speed impact?)
  • Check enum values every time data is read and panic (speed impact)
  • Do not expose as enum, but newtype integer plus constants
  • If speed is impacted a raw reading function might be needed for fast access
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rust
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant