Skip to content

Commit

Permalink
Fix panic with invalid unicode query (#645)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
LegNeato authored May 1, 2020
1 parent 52aea4d commit 358ca27
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion juniper/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ impl<'a> Lexer<'a> {
len += 1;
}

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

if len != 4 {
return Err(Spanning::zero_width(
Expand Down
9 changes: 9 additions & 0 deletions juniper/src/parser/tests/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ fn string_errors() {
LexerError::UnterminatedString
)
);

// Found by fuzzing.
assert_eq!(
tokenize_error(r#""\uɠ^A"#),
Spanning::zero_width(
&SourcePosition::new(5, 0, 5),
LexerError::UnterminatedString
)
);
}

#[test]
Expand Down

0 comments on commit 358ca27

Please sign in to comment.