Skip to content

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

Closed
@huonw

Description

@huonw

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions