Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/stringparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,16 @@ pub(crate) fn get_unicode_codepoint(mut src_ptr: &[u8]) -> Result<(u32, usize),
if (unsafe { *src_ptr.get_kinda_unchecked(0) } != b'\\')
|| unsafe { *src_ptr.get_kinda_unchecked(1) } != b'u'
{
return Ok((0, src_offset));
// A high surrogate must be followed by a `\u` low surrogate.
return Err(ErrorType::InvalidUnicodeCodepoint);
}

let code_point_2: u32 = hex_to_u32_nocheck(unsafe { src_ptr.get_kinda_unchecked(2..) });

// if the first code point is invalid we will get here, as we will go past
// the check for being outside the Basic Multilingual plane. If we don't
// find a \u immediately afterwards we fail out anyhow, but if we do,
// this check catches both the case of the first code point being invalid
// or the second code point being invalid.
if ((code_point | code_point_2) >> 16) != 0 {
return Ok((0, src_offset));
// Invalid hex digits set the high bits, which also fall outside this range.
if !LOW_SURROGATES.contains(&code_point_2) {
return Err(ErrorType::InvalidUnicodeCodepoint);
}
let Some(c1) = code_point.checked_sub(0xd800) else {
return Err(ErrorType::InvalidUtf8);
};
let Some(c2) = code_point_2.checked_sub(0xdc00) else {
return Err(ErrorType::InvalidUtf8);
};
code_point = ((c1 << 10) | c2) + 0x10000;
code_point = (((code_point - 0xd800) << 10) | (code_point_2 - 0xdc00)) + 0x10000;
src_offset += 6;
} else if LOW_SURROGATES.contains(&code_point) {
// This is a low surrogate on it's own, which is invalid.
Expand Down
41 changes: 41 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,44 @@ proptest! {
}

}

#[test]
fn high_surrogate_without_a_low_surrogate_is_an_error() {
for bad in [
r#""\ud800""#,
r#""\ud800x""#,
r#"["\udbff"]"#,
r#"{"k":"a\ud800b"}"#,
r#""\ud800\n""#,
r#""\ud800\uzzzz""#,
r#""\ud800\u0041""#,
r#""\ud800\udbff""#,
r#""\ud800\ue000""#,
r#""\ud800\uffff""#,
r#""\udbff\ue000""#,
r#""\udbff\uffff""#,
r#""\udc00""#,
] {
let mut v = bad.as_bytes().to_vec();
assert!(crate::to_tape(&mut v).is_err(), "accepted: {bad}");
let mut v = bad.as_bytes().to_vec();
assert!(crate::to_borrowed_value(&mut v).is_err(), "accepted: {bad}");
#[cfg(feature = "serde_impl")]
{
let mut v = bad.as_bytes().to_vec();
assert!(
crate::serde::from_slice::<String>(&mut v).is_err(),
"accepted: {bad}"
);
}
}
for (good, decoded) in [
(r#""a\ud83d\ude00b""#, "a\u{1F600}b"),
(r#""\ud800\udc00""#, "\u{10000}"),
(r#""\udbff\udfff""#, "\u{10FFFF}"),
] {
let mut v = good.as_bytes().to_vec();
let t = crate::to_tape(&mut v).expect("valid surrogate pair");
assert_eq!(t.0[0], crate::Node::String(decoded));
}
}