ion-rust
ion-rust copied to clipboard
Panic when using `format!` and debug output for a nested value with a bad escape character in the LazyReader.
#[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