Skip to content

Index-assign in str should be unsafe #8891

Closed
@SimonSapin

Description

@SimonSapin

As far as I understand, the str types have a pretty strong assumption that they contain valid UTF-8. Any method/function that could break this (such as push_bytes) is marked as unsafe.

However, it is possible to assign random bytes in the middle of a ~str, breaking the UTF-8 invariant:

rusti: let mut a = ~"test"; a[1] = 0x80; 
        (a.as_bytes().to_owned(), ::std::str::is_utf8(a.as_bytes()))
(~[116, 128, 115, 116], false)

I think that such assignments should only be allowed in unsafe code.

Update: str.as_mut_buf should also be unsafe, IMO.

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