Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enforce Src: FromBytes in try_transmute_mut! #2229

Merged
merged 1 commit into from
Feb 4, 2025
Merged

Conversation

jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Jan 12, 2025

Ensures that the source reference remains valid after the transmuted (and possibly mutated) destination is dropped.

Makes progress on #2226

@jswrenn jswrenn requested a review from joshlf January 12, 2025 00:34
@jswrenn jswrenn force-pushed the fix-try_transmute_mut branch from 69ad562 to 2bf3099 Compare January 12, 2025 00:39
@jswrenn
Copy link
Collaborator Author

jswrenn commented Jan 12, 2025

(Have a killer headache tonight; I'll finish this up first thing tomorrow.)

@jswrenn jswrenn force-pushed the fix-try_transmute_mut branch from 2bf3099 to 4fdd646 Compare January 12, 2025 12:53
@codecov-commenter
Copy link

codecov-commenter commented Jan 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.46%. Comparing base (2c8ef74) to head (018a552).
Report is 10 commits behind head on v0.8.x.

Additional details and impacted files
@@            Coverage Diff             @@
##           v0.8.x    #2229      +/-   ##
==========================================
+ Coverage   87.42%   87.46%   +0.04%     
==========================================
  Files          16       16              
  Lines        6115     6136      +21     
==========================================
+ Hits         5346     5367      +21     
  Misses        769      769              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jswrenn jswrenn force-pushed the fix-try_transmute_mut branch from 4fdd646 to cc95f58 Compare January 27, 2025 22:41
src/macros.rs Outdated
@@ -577,7 +577,7 @@ macro_rules! try_transmute_ref {
/// ```ignore
/// fn try_transmute_mut<Src, Dst>(src: &mut Src) -> Result<&mut Dst, ValidityError<&mut Src, Dst>>
/// where
/// Src: IntoBytes,
/// Src: FromByte + IntoBytes,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Src: FromByte + IntoBytes,
/// Src: FromBytes + IntoBytes,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/macros.rs Outdated
Comment on lines 891 to 894
let array_of_bools = &mut [false, true, false, true, false, true, false, true];
let array_of_arrays = &mut [[0u8, 1], [0, 1], [0, 1], [0, 1]];
let x: Result<&mut [[u8; 2]; 4], _> = try_transmute_mut!(array_of_bools);
assert_eq!(x, Ok(array_of_arrays));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep this test and use u8 instead of bool?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensures that the source reference remains valid after the
transmuted (and possibly mutated)  destination is dropped.

Makes progress on #2226
@jswrenn jswrenn force-pushed the fix-try_transmute_mut branch from cc95f58 to 018a552 Compare February 4, 2025 16:53
@jswrenn jswrenn requested a review from joshlf February 4, 2025 16:55
@jswrenn jswrenn enabled auto-merge February 4, 2025 16:55
@jswrenn jswrenn added this pull request to the merge queue Feb 4, 2025
Merged via the queue into v0.8.x with commit 118b6f3 Feb 4, 2025
87 checks passed
@jswrenn jswrenn deleted the fix-try_transmute_mut branch February 4, 2025 17:58
joshlf pushed a commit that referenced this pull request Feb 14, 2025
Ensures that the source reference remains valid after the
transmuted (and possibly mutated)  destination is dropped.

Makes progress on #2226

gherrit-pr-id: I425e7d5103cb3b2a9e7107bf9df0743dca2e08cb
@joshlf
Copy link
Member

joshlf commented Feb 14, 2025

Backporting in #2343

github-merge-queue bot pushed a commit that referenced this pull request Feb 14, 2025
…ryFromBytes::try_mut*` (#2343)

* Enforce `Src: FromBytes` in `try_transmute_mut!` (#2229)

Ensures that the source reference remains valid after the
transmuted (and possibly mutated)  destination is dropped.

Makes progress on #2226

gherrit-pr-id: I425e7d5103cb3b2a9e7107bf9df0743dca2e08cb

* Add `Self: IntoBytes` bound to `TryFromBytes::try_mut*`

Consider that `MaybeUninit<u8>` is `TryFromBytes`. If a `&mut [u8]`
is cast into a `&mut MaybeUninit<u8>`, then uninit bytes are written,
the shadowed `&mut [u8]`'s referent will no longer be valid.

Makes progress towards #2226 and #1866.

gherrit-pr-id: Ib233c4d0643e0690c53a37a08d9845e5fe43249c

---------

Co-authored-by: Jack Wrenn <jswrenn@amazon.com>
Co-authored-by: Jack Wrenn <jack@wrenn.fyi>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants