ion-rust icon indicating copy to clipboard operation
ion-rust copied to clipboard

Panic when using `format!` and debug output for a nested value with a bad escape character in the LazyReader.

Open jpschorr opened this issue 1 year ago • 0 comments

#[test]
fn blow_up() {
    // At the top level, the bad escape character causes an error before I can try to debug format anything
    let bad_str = r#" "[1-9]\d+" "#;

    let mut reader = LazyReader::new(bad_str.as_bytes());
    let val: LazyValue<AnyEncoding> = reader.next().unwrap().unwrap();
    let read = val.read();
    // the bad string causes the `read` to error
    assert!(read.is_err());

    // If I nest the bad string, I can get `format!("Blows up: {read:?}")` to panic
    let bad = format!(r#"{{ nested: {bad_str} }}"#);

    let mut reader = LazyReader::new(bad.as_bytes());
    let val: LazyValue<AnyEncoding> = reader.next().unwrap().unwrap();
    let read = val.read();
    // nesting the bad string causes the `read` to ok
    assert!(read.is_ok());
    let read = read.unwrap();

    // dbg! works
    dbg!(&read);
    // But formating with `:?` causes a panic:
    //       a formatting trait implementation returned an error: Error
    format!("Blows up: {read:?}");
}

The panic is in std::fmt::format: https://doc.rust-lang.org/src/alloc/fmt.rs.html#633

jpschorr avatar May 16 '24 22:05 jpschorr