Add fuzz test and fix it.

This commit is contained in:
Ethiraric 2024-10-19 18:53:51 +02:00
parent e052a63458
commit d997b53c8d
2 changed files with 44 additions and 2 deletions

View file

@ -2249,7 +2249,7 @@ impl<T: Input> Scanner<T> {
} }
// Process blank characters. // Process blank characters.
self.input.lookahead(1); self.input.lookahead(2);
while self.input.next_is_blank_or_break() { while self.input.next_is_blank_or_break() {
if self.input.next_is_blank() { if self.input.next_is_blank() {
if !self.leading_whitespace { if !self.leading_whitespace {
@ -2280,7 +2280,7 @@ impl<T: Input> Scanner<T> {
self.leading_whitespace = true; self.leading_whitespace = true;
} }
} }
self.input.lookahead(1); self.input.lookahead(2);
} }
// check indentation level // check indentation level

42
parser/tests/fuzz.rs Normal file
View file

@ -0,0 +1,42 @@
use core::str;
use saphyr_parser::{Event, Parser, ScanError};
/// Run the parser through the string.
///
/// The parser is run through both the `StrInput` and `BufferedInput` variants. The resulting
/// events are then compared and must match.
///
/// # Returns
/// This function returns the events if parsing succeeds, the error the parser returned otherwise.
///
/// # Panics
/// This function panics if there is a mismatch between the 2 parser invocations with the different
/// input traits.
fn run_parser(input: &str) -> Result<Vec<Event>, ScanError> {
let mut str_events = vec![];
let mut iter_events = vec![];
for x in Parser::new_from_str(input) {
str_events.push(x?.0);
}
for x in Parser::new_from_iter(input.chars()) {
iter_events.push(x?.0);
}
assert_eq!(str_events, iter_events);
Ok(str_events)
}
#[test]
fn fuzz_1() {
// Crashing with an index out-of-bounds error.
// In `scan_plain_scalar`, we would lookahead 1 and call `skip_break`, which requires a
// lookahead of 2.
let raw_input: &[u8] = &[
1, 39, 110, 117, 108, 108, 34, 13, 13, 13, 13, 13, 10, 13, 13, 13, 13,
];
let s = str::from_utf8(raw_input).unwrap();
let _ = run_parser(s);
}