[layout] Restrict CastFrom's CastExact impl - #3549
Open
enthropy7 wants to merge 1 commit into
Open
Conversation
CastFrom preserves unpadded size, but lowering the alignment of a dynamically padded DST can shrink its referent. Require IntoBytes on the source so CastExact is only implemented for padding-free source layouts. Add a regression test using an aligned slice DST whose one-byte payload projects from an eight-byte referent to a one-byte slice.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3549 +/- ##
=======================================
Coverage 91.85% 91.86%
=======================================
Files 20 20
Lines 6093 6100 +7
=======================================
+ Hits 5597 5604 +7
Misses 496 496 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
CastExactpromises that a cast preserves the complete referent byte range. However,CastFromonly preserves the address and the size before trailing padding. This is incorrect for dynamically sized types with explicit alignment:An
Aligned<[u8]>containing one byte has a total size of eight bytes because of trailing padding. Projecting it to[u8]produces a one-byte referent. This is a valid shrinkingCast, but not aCastExact.тThis case came up while checking complete byte ranges in aligned zero-copy video buffers, instead of checking only addresses and logical payload lengths.This PR restricts the
CastExactimplementation toSrc: IntoBytes. Such sources cannot contain padding, so the complete byte range is preserved. The generalCastimplementation remains available for projections that may shrink.No current safe public path to UB was found because the existing transmute paths already require
Src: IntoBytes. The change is still needed because the unsafeCastExactimplementation must be correct for every type for which it is implemented. Otherwise, future internal code could rely on a false size-preservation guarantee.The regression test demonstrates the eight-to-one-byte projection and verifies that it implements
Cast, but notCastExact.Tested with the repository's MSRV, stable, and nightly configurations, including Clippy, formatting, and all 136 library tests.
Related to #2910 and #3199.