Skip to content

Commit

Permalink
Reject corpus which produces empty range for LALRPOP parser (#10876)
Browse files Browse the repository at this point in the history
## Summary

This PR updates the `ruff_new_parser_equiv` fuzzer target to reject the
corpus which produces an empty range for the AST from LALRPOP. If there
are no nodes, then LALRPOP produces an empty range while the new parser
uses the correct range of the source itself.
  • Loading branch information
dhruvmanila authored Apr 11, 2024
1 parent 301ba35 commit b069358
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions fuzz/fuzz_targets/ruff_new_parser_equiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,27 @@ fn do_fuzz(case: &[u8]) -> Corpus {
return Corpus::Keep; // keep even rejected source code as this may allow us to explore later
};

set_new_parser(true);
let module_new = parse_tokens(tokens.clone(), source, source_type.as_mode());

set_new_parser(false);
let module_lalrpop = parse_tokens(tokens, source, source_type.as_mode());
let result_lalrpop = parse_tokens(tokens.clone(), source, source_type.as_mode());

if result_lalrpop.as_ref().is_ok_and(|ast_lalrpop| {
ast_lalrpop
.as_module()
.is_some_and(|module_lalrpop| module_lalrpop.range.is_empty())
}) {
// Reject the corpus which only contains whitespaces, comments, and newlines. This is
// because the module range produced by LALRPOP is empty while it is the source length in
// case of the new parser.
return Corpus::Reject;
}

set_new_parser(true);
let result_new = parse_tokens(tokens, source, source_type.as_mode());

assert_eq!(module_lalrpop.is_ok(), module_new.is_ok());
assert_eq!(result_lalrpop.is_ok(), result_new.is_ok());

if let (Ok(module_lalrpop), Ok(module_new)) = (module_lalrpop, module_new) {
assert_eq!(module_lalrpop, module_new);
if let (Ok(ast_lalrpop), Ok(ast_new)) = (result_lalrpop, result_new) {
assert_eq!(ast_lalrpop, ast_new);
}

Corpus::Keep
Expand Down

0 comments on commit b069358

Please sign in to comment.