Skip to content

Add SAFETY comments to RawValue's unsafe transmutes - #1349

Open
choudhryfrompak wants to merge 1 commit into
serde-rs:masterfrom
choudhryfrompak:fix/raw-value-safety-comments
Open

choudhryfrompak wants to merge 1 commit into
serde-rs:masterfrom
choudhryfrompak:fix/raw-value-safety-comments

Conversation

@choudhryfrompak

Copy link
Copy Markdown

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.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant