Skip to content

Commit

Permalink
Allow NUL
Browse files Browse the repository at this point in the history
  • Loading branch information
torkleyy committed Feb 13, 2018
1 parent 4ad241a commit 87f3bd7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,9 @@ impl<'a> Bytes<'a> {
self.expect_byte(b'{', ParseError::InvalidEscape("Missing {"))?;

let mut bytes: u32 = 0;
let mut num_digits = 0;

for _ in 0..6 {
while num_digits < 6 {
let byte = self.peek_or_eof()?;

if byte == b'}' {
Expand All @@ -481,11 +482,13 @@ impl<'a> Bytes<'a> {
let byte = self.decode_hex(byte)?;
bytes = bytes << 4;
bytes |= byte as u32;

num_digits += 1;
}

if bytes == 0 {
if num_digits == 0 {
return self.err(ParseError::InvalidEscape(
"Null character (\\u{0}) not allowed",
"Expected 1-6 digits, got 0 digits",
));
}

Expand Down
5 changes: 5 additions & 0 deletions tests/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ fn test_chars() {
assert_eq!(to_string(&'\u{715}').unwrap(), "'\u{715}'");
assert_eq!(from_str::<char>("'\u{715}'").unwrap(), from_str("'\\u{715}'").unwrap());
}

#[test]
fn test_nul_in_string() {
check_same("Hello\0World!".to_owned());
}

0 comments on commit 87f3bd7

Please sign in to comment.