Repository navigation
Add SAFETY comments to RawValue's unsafe transmutes - #1349
Open
choudhryfrompak wants to merge 1 commit into
Open
choudhryfrompak wants to merge 1 commit into
choudhryfrompak wants to merge 1 commit into
Conversation
RawValue::from_borrowed/from_owned/into_owned each use mem::transmute without explaining why the layout change is sound, unlike every other unsafe block in this crate. Document the repr(transparent) and drop-glue reasoning that makes each one safe. No behavior change.
This branch has not been deployed
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.
RawValue::from_borrowed, from_owned, and into_owned (src/raw.rs) each use mem::transmute with no comment explaining why it's sound. Every other unsafe block in the crate I could find (ser.rs, read.rs) has one, so this looked like an oversight rather than a deliberate omission.
Added a SAFETY comment above each of the three transmutes. from_borrowed and from_owned/into_owned both rest on RawValue being #[repr(transparent)] around a single str field, so &str/&RawValue and Box/Box share layout. For the Box cases I also noted that RawValue has no Drop impl of its own, so the box's drop glue (drop the pointee, deallocate) doesn't change across the transmute.
No behavior change, just documentation. Added a small test (tests/regression/raw_value_transmute_safety.rs) that exercises all three paths through the public API — RawValue::NULL/TRUE/FALSE (from_borrowed), from_string (from_owned), and Box -> Box (into_owned) — and checks the JSON text comes out unchanged.
cargo fmt --check and cargo clippy --all-features --all-targets are clean, and cargo test (default features and --all-features) passes with the same counts as master plus this one new test.