Skip to content

Commit

Permalink
Merge pull request #2 from azuqua/fix/json-stringify-panic
Browse files Browse the repository at this point in the history
backtrack on invalid character boundaries, pt 2
  • Loading branch information
alecembke-okta authored May 15, 2019
2 parents 5043053 + 0561bd3 commit a1430c7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "json"
version = "0.11.16"
version = "0.11.17"
authors = ["Maciej Hirsz <[email protected]>", "Alec Embke <[email protected]>"]
description = "JSON implementation in Rust"
repository = "https://github.com/azuqua/json-rust"
Expand Down
18 changes: 13 additions & 5 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,13 @@ mod gen_test {

let mut generator = DumpGenerator::new();
generator.write_string(&input);

println!("output string: {}", String::from_utf8_lossy(&generator.code));
}

#[test]
fn should_panic_on_bad_bytes() {
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]
Expand All @@ -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);
}

}
Expand Down Expand Up @@ -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'"')
Expand Down

0 comments on commit a1430c7

Please sign in to comment.