Skip to content

String::retain can result in invalid utf-8 on panic #261

Open
@Darksonn

Description

@Darksonn

This safe code creates a string containing invalid utf-8, which is undefined behavior:

use bumpalo::{Bump, collections::string::String};
use std::panic::AssertUnwindSafe;

fn main() {
    let bump = Bump::new();
    let mut s = String::new_in(&bump);

    s.push_str("_få_b");
    std::panic::catch_unwind(AssertUnwindSafe(|| {
        s.retain(|c| {
            if c == 'b' {
                panic!();
            }
            c != '_'
        });
    }));


    println!("{:?}", core::str::from_utf8(s.as_bytes()));
}
Err(Utf8Error { valid_up_to: 3, error_len: Some(1) })

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions