From ab670b4e9f9cc7628f27eede802eeb32495ea933 Mon Sep 17 00:00:00 2001 From: Jihyun Yu Date: Sat, 29 Sep 2018 15:39:58 +0900 Subject: [PATCH] fix bug on error position --- src/read.rs | 9 ++++++--- tests/test.rs | 28 ++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/read.rs b/src/read.rs index f9fea9d..2e5b4a8 100644 --- a/src/read.rs +++ b/src/read.rs @@ -455,10 +455,10 @@ impl<'a> SliceRead<'a> { start = self.index; } _ => { + self.index += 1; if validate { return error(self, ErrorCode::ControlCharacterWhileParsingString); } - self.index += 1; } } } @@ -548,17 +548,20 @@ impl<'a> Read<'a> for SliceRead<'a> { fn decode_hex_escape(&mut self) -> Result { if self.index + 4 > self.slice.len() { + self.index = self.slice.len(); return error(self, ErrorCode::EofWhileParsingString); } + let mut n = 0; for _ in 0..4 { - match decode_hex_val(self.slice[self.index]) { + let ch = decode_hex_val(self.slice[self.index]); + self.index += 1; + match ch { None => return error(self, ErrorCode::InvalidEscape), Some(val) => { n = (n << 4) + val; } } - self.index += 1; } Ok(n) } diff --git a/tests/test.rs b/tests/test.rs index ec2de42..954ac90 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -995,11 +995,11 @@ fn test_parse_string() { ), ( "\"\n\"", - "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 1", + "control character (\\u0000-\\u001F) found while parsing a string at line 2 column 0", ), ( "\"\x1F\"", - "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 1", + "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 2", ), ]); @@ -1012,13 +1012,33 @@ fn test_parse_string() { &[b'"', b'\\', b'n', 159, 146, 150, b'"'], "invalid unicode code point at line 1 column 7", ), + ( + &[b'"', b'\\', b'u', 48, 48, 51], + "EOF while parsing a string at line 1 column 6", + ), + ( + &[b'"', b'\\', b'u', 250, 48, 51, 48, b'"'], + "invalid escape at line 1 column 4", + ), + ( + &[b'"', b'\\', b'u', 48, 250, 51, 48, b'"'], + "invalid escape at line 1 column 5", + ), + ( + &[b'"', b'\\', b'u', 48, 48, 250, 48, b'"'], + "invalid escape at line 1 column 6", + ), + ( + &[b'"', b'\\', b'u', 48, 48, 51, 250, b'"'], + "invalid escape at line 1 column 7", + ), ( &[b'"', b'\n', b'"'], - "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 1", + "control character (\\u0000-\\u001F) found while parsing a string at line 2 column 0", ), ( &[b'"', b'\x1F', b'"'], - "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 1", + "control character (\\u0000-\\u001F) found while parsing a string at line 1 column 2", ), ]);