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

{String,Vec}::from_raw_parts docs need to be much clearer about incorrectness #26737

Closed
huonw opened this issue Jul 2, 2015 · 0 comments
Closed

Comments

@huonw
Copy link
Member

huonw commented Jul 2, 2015

The docs for these are currently fairly meagre:

unsafe fn from_raw_parts(buf: *mut u8, length: usize, capacity: usize) -> String

Creates a new String from a length, capacity, and pointer.

This is unsafe because:

  • We call Vec::from_raw_parts to get a Vec<u8>;
  • We assume that the Vec contains valid UTF-8.

unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Vec

Creates a Vec<T> directly from the raw components of another vector.

This is highly unsafe, due to the number of invariants that aren't checked.

They need to be more specific about the details, e.g.:

  • ptr needs to have been previously allocated via String/Vec (at least, it's highly likely to be incorrect if it wasn't)
  • capacity needs to be the capacity that the pointer was allocated with.

Violating these may cause problems like corrupting the allocators internal datastructures.

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

1 participant