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

CompactString::as_mut_bytes returns a &mut reference to uninitialized bytes #326

Open
goffrie opened this issue Oct 15, 2023 · 1 comment

Comments

@goffrie
Copy link

goffrie commented Oct 15, 2023

e.g. unsafe { CompactString::with_capacity(1234).as_mut_bytes() } returns a slice of length 1234. This is questionable (even if it hasn't been declared officially UB yet) and IMO should return a &mut [MaybeUninit<u8>], e.g. by the precedent set by Vec::spare_capacity_mut.

Also, the documentation is not very clear that the returned slice includes those uninitialized bytes (i.e. those past len up to capacity), and doesn't disclose that there may be uninitialized bytes hiding in the result. It's especially confusing since the documentation lists the methods in order as as_str(), as_mut_str(), as_bytes(), and as_mut_bytes() - but only the last one reveals the uninitialized part.

@ParkMyCar
Copy link
Owner

Great callout! The as_mut_bytes(...) API definitely needs to be improved (or removed?), from a types and documentation perspective. I'll try and get to this before we release v0.8.0 since it would be a public API change.

Not sure if the API for as_mut_bytes(...) should only returns the bytes up-to len, or if we keep the current behavior and just document it. I'll need to think on this/look at what String and others do.

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