Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic deserializing RawValue from invalid utf-8 bytes #757

Merged
merged 2 commits into from Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/read.rs
Expand Up @@ -587,7 +587,10 @@ impl<'a> Read<'a> for SliceRead<'a> {
V: Visitor<'a>,
{
let raw = &self.slice[self.raw_buffering_start_index..self.index];
let raw = str::from_utf8(raw).unwrap();
let raw = match str::from_utf8(raw) {
Ok(raw) => raw,
Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
};
visitor.visit_map(BorrowedRawDeserializer {
raw_value: Some(raw),
})
Expand Down
19 changes: 19 additions & 0 deletions tests/test.rs
Expand Up @@ -2201,6 +2201,25 @@ fn test_boxed_raw_value() {
assert_eq!(r#"["a",42,{"foo": "bar"},null]"#, array_to_string);
}

#[cfg(feature = "raw_value")]
#[test]
fn test_raw_invalid_utf8() {
use serde_json::value::RawValue;

let j = &[b'"', b'\xCE', b'\xF8', b'"'];
let value_err = serde_json::from_slice::<Value>(j).unwrap_err();
let raw_value_err = serde_json::from_slice::<Box<RawValue>>(j).unwrap_err();

assert_eq!(
value_err.to_string(),
"invalid unicode code point at line 1 column 4",
);
assert_eq!(
raw_value_err.to_string(),
"invalid unicode code point at line 1 column 4",
);
}

#[test]
fn test_borrow_in_map_key() {
#[derive(Deserialize, Debug)]
Expand Down