Skip to content

str::buf_as_slice includes (and reads) uninitialized memory in the slice #3843

Closed
@brson

Description

@brson

As long as string slices expect to have an extra byte at the end this function cannot be expressed without copying. Here's how it's written:

1964-    /// Form a slice from a *u8 buffer of the given length without copying.
1965:    pub unsafe fn buf_as_slice<T>(buf: *u8, len: uint,
1966-                              f: fn(v: &str) -> T) -> T {
1967-        let v = (buf, len + 1);
1968-        assert is_utf8(::cast::reinterpret_cast(&v));
1969-        f(::cast::transmute(move v))
1970-    }
1971-

That extra byte in len + 1 is just random memory, which is_utf8 immediately reads.

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