Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow creating decoder over raw slice pointer without aliasing #644

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tremwil
Copy link

@tremwil tremwil commented Dec 12, 2024

Currently, the Decoder keeps a reference to the data slice passed in constructors. This means that there is no way to unsafely create a decoder over a partially valid or aliased memory range (where we assert that the specific subslices at which we'll be decoding are safe to read) without immediately causing UB. Because the decoder uses raw pointers internally in its logic, this seems like an unnecessary restriction which forces re-creating decoders every time we want to decode at an arbitrary address.

This PR adds an unsafe constructor, try_with_slice_ptr, which lets one construct a decoder from a raw slice pointer. To avoid aliasing, the data field in the Decoder struct is replaced by a slice pointer, and the reference is moved inside a PhantomData.

Since the slice_ptr_len feature was only stabilized in 1.79, this does require bumping the MSRV, which I understand is probably not desirable. In this case the constructor could be replaced by try_with_raw_parts which takes a *const u8 and length instead.

@wtfsck
Copy link
Member

wtfsck commented Dec 29, 2024

Yeah 1.79 is too recent so can't merge anything that requires it. I'll bump the msrv but not to 1.79.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants