From 0561bd301a6d9fafea0a089004581ad18628f34b Mon Sep 17 00:00:00 2001 From: Alec Embke Date: Wed, 15 May 2019 09:00:45 -0700 Subject: [PATCH] backtrack on invalid character boundaries, pt 2 --- Cargo.toml | 2 +- src/codegen.rs | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a903072..e3dcbe9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "json" -version = "0.11.16" +version = "0.11.17" authors = ["Maciej Hirsz ", "Alec Embke "] description = "JSON implementation in Rust" repository = "https://github.com/azuqua/json-rust" diff --git a/src/codegen.rs b/src/codegen.rs index d8c0a56..1370985 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -54,8 +54,6 @@ mod gen_test { let mut generator = DumpGenerator::new(); generator.write_string(&input); - - println!("output string: {}", String::from_utf8_lossy(&generator.code)); } #[test] @@ -63,8 +61,6 @@ mod gen_test { let mut all = "`�^S^R�^]^?^@^@E BOONE TRL "; let mut generator = DumpGenerator::new(); generator.write_string_complex(all, 3); - - println!("output string: {}", String::from_utf8_lossy(&generator.code)); } #[test] @@ -74,8 +70,16 @@ mod gen_test { let mut generator = DumpGenerator::new(); generator.write_string(&input); + } - println!("output string: {}", String::from_utf8_lossy(&generator.code)); + #[test] + fn should_not_panic_on_bad_bytes_3() { + let data = b"\x48\x48\x48\x57\x03\xE8\x48\x48\xE8\x03\x8F\x48\x29\x48\x48"; + let s = unsafe { + String::from_utf8_unchecked(data.to_vec()) + }; + let mut generator = DumpGenerator::new(); + generator.write_string(&s); } } @@ -104,6 +108,10 @@ pub trait Generator { try!(write!(self.get_writer(), "{:04x}", ch)); } } + + while !string.is_char_boundary(start) && start > 0 { + start -= 1; + } try!(self.write(string[start ..].as_bytes())); self.write_char(b'"')