Skip to content

Incorrect const-ness on CString::new argument #18117

Closed
@canndrew

Description

@canndrew

The signature of CString::new is CString::new(buf: *const i8, owns_buffer: bool). The problem with this is that if the new CString does own the buffer it's going to free it. In which case buf should be a *mut i8 not a *const i8. However making buf: *mut i8 doesn't account for the case where the CString doesn't own the buffer and the buffer is immutable.

To fix this CString::new should be split into two functions:

unsafe fn CString::new(buf: *const i8) -> CString;
unsafe fn CString::new_owned(buf: *mut i8) -> CString;

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