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

up do date minibytes crate #897

Closed
somethingelseentirely opened this issue May 22, 2024 · 5 comments
Closed

up do date minibytes crate #897

somethingelseentirely opened this issue May 22, 2024 · 5 comments

Comments

@somethingelseentirely
Copy link

somethingelseentirely commented May 22, 2024

The minibytes sub-library is way too elegant and useful to be hidden away. It would be great if it was published regularly so that a recent version can just be pulled from crates.io.

@somethingelseentirely somethingelseentirely changed the title minibytes crate up do date minibytes crate May 22, 2024
@somethingelseentirely
Copy link
Author

Just as an upgrade, I've decided to create a fork that tries to combine the best aspect of minibytes and ownedbytes called anybytes.

I've just discovered some UB in my library that I think also affects minibytes.
When a Bytes is created from a Vec<u8> as the ByteOwner, then the reference returned fromdowncast_mut might be reallocated when the Vector is resized. Since the pointer in the Bytes themselves is not invalidated it can result in a use after free situation.

I don't really need a recent minibytes release anymore, so feel free to close this issue.

Thank you so much for your work and take care!

@quark-zju
Copy link
Contributor

Sorry for the late reply and thanks for spotting the UB! It seems we don't use downcast_mut outside minibytes itself. So perhaps we can just mark it as a private function.

@somethingelseentirely
Copy link
Author

somethingelseentirely commented Sep 26, 2024

I think that would be an option, although the act of handing out a & and &mut at the same time alone is probably UB.
I changed the API to return an Arc<T>, where T is the type downcast to. That way you can only get mutability if all Bytes with that owner have been dropped.

@quark-zju
Copy link
Contributor

handing out a & and &mut at the same time

Could you give an example about when that can happen? Given that:

  • downcast_mut takes &mut self which means no as_ref() slices are handed out.
  • The struct uses *const u8 instead of &'static [u8] (like ownerbytes) so there is no in-struct & and &mut being alive at the same time.

It's unclear to me how we have & &mut borrows both alive at the same time.

@somethingelseentirely
Copy link
Author

Yeah I think you're right, sorry my bad!

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

No branches or pull requests

2 participants