seal Buf trait and make dst() method unsafe for soundness and safety#8
Merged
cfcosta merged 1 commit intocfcosta:mainfrom Nov 30, 2025
Merged
seal Buf trait and make dst() method unsafe for soundness and safety#8cfcosta merged 1 commit intocfcosta:mainfrom
cfcosta merged 1 commit intocfcosta:mainfrom
Conversation
Owner
|
Sorry about the delay, have been traveling for the past few weeks. I'm back now :D |
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.
Problem
The
Buftrait was unsound. It allowed transmuting&mut [u8]to&mut [MaybeUninit<u8>]through a safe interface, which could be exploited to cause undefined behavior.Solution
This PR makes the trait sound using the sealed trait pattern:
private::Sealed- Empty trait in private module, implemented only for[u8]and[MaybeUninit<u8>]Buf: private::Sealed- Public trait requires the sealed trait, preventing external implementationsunsafe fn dst()- Method marked unsafe with documented safety requirement: callers must only write initialized bytesSafety Guarantee
The library maintains the invariant that only fully initialized bytes are written through
dst():encode_to_bufwrites throughencode_simd_*andencode_scalar, all of which write initialized bytesdecode_to_bufwrites throughdecode_into, which writes initialized bytes