hardening: mark decode_fixed_vec_le as unsafe#19
Open
MegaRedHand wants to merge 7 commits into
Open
Conversation
Compute size_of::<T>() inside the function instead of taking it as a parameter. This makes a size mismatch impossible by construction, replacing the debug_assert added in the previous commit.
Split into a doc-level safety contract (what callers must uphold) and a block-level SAFETY comment (why the operations are sound).
The function has a safety contract that the type system cannot enforce (T must have no padding and be valid for any bit pattern), so callers should explicitly acknowledge it via an unsafe block.
Split the doc comment into a Safety section (valid-for-any-bit-pattern, the actual soundness requirement) and a Correctness section (little-endian layout, enforced by cfg).
Split the doc comment into a Safety section (valid-for-any-bit-pattern, the actual soundness requirement) and a Correctness section (little-endian layout, enforced by cfg).
decode_fixed_vec_ledecode_fixed_vec_le as unsafe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR marks
decode_fixed_vec_leasunsafe, documenting the safety and correctness requirements, which were already upheld. It also removes the second parameter, since it could be computed inside the function, and doing so simplifies safety requirements.The reason the function is unsafe is only because some types have invalid values, which can be represented in the raw byte format, but don't really have a representable value in the type. See "Producing invalid values" in the Rustonomicon
For example,
boolcan only betrueorfalse, which are1and0, respectively. However, interpreting an arbitrary byte (boolhas a size of 1 byte) as aboolcould result in interpreting unrepresentable values like2, `3, etc.