Skip to content

Commit 358ca27

Browse files
authored
Fix panic with invalid unicode query (#645)
Without this fix the panic looks like the following: ```rust ---- parser::tests::lexer::string_errors stdout ---- thread 'parser::tests::lexer::string_errors' panicked at 'byte index 4 is not a char boundary; it is inside 'ɠ' (bytes 3..5) of `"\uɠ^A`', src/libcore/str/mod.rs:2219:5 ``` This was found via fuzzing with `cargo-fuzz`.
1 parent 52aea4d commit 358ca27

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

juniper/src/parser/lexer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ impl<'a> Lexer<'a> {
296296
len += 1;
297297
}
298298

299-
let escape = &self.source[start_idx..=end_idx];
299+
// Make sure we are on a valid char boundary.
300+
let escape = &self
301+
.source
302+
.get(start_idx..=end_idx)
303+
.ok_or_else(|| Spanning::zero_width(&self.position, LexerError::UnterminatedString))?;
300304

301305
if len != 4 {
302306
return Err(Spanning::zero_width(

juniper/src/parser/tests/lexer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ fn string_errors() {
322322
LexerError::UnterminatedString
323323
)
324324
);
325+
326+
// Found by fuzzing.
327+
assert_eq!(
328+
tokenize_error(r#""\uɠ^A"#),
329+
Spanning::zero_width(
330+
&SourcePosition::new(5, 0, 5),
331+
LexerError::UnterminatedString
332+
)
333+
);
325334
}
326335

327336
#[test]

0 commit comments

Comments
 (0)