Skip to content

CString::from_raw should be document that changing the length of a string is UB #68456

Closed
@KamilaBorowska

Description

@KamilaBorowska

Consider the following code:

use std::ffi::CString;

fn main() {
    let c = CString::new(&b"Hello, world"[..]).unwrap();
    let ptr = c.into_raw();
    unsafe {
        *ptr = 0;
        CString::from_raw(ptr); // UB
    }
}

Shortening the string from CString::into_raw and then passing it to CString::from_raw will cause UB, but this is not documented in safety section of the documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions