From dc4cddb2adfb09b2b68cb9b1559493077d786311 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 11 Apr 2024 17:58:34 +0530 Subject: [PATCH] Reject corpus which produces empty range for LALRPOP parser (#10876) ## 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. --- fuzz/fuzz_targets/ruff_new_parser_equiv.rs | 25 ++++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/fuzz/fuzz_targets/ruff_new_parser_equiv.rs b/fuzz/fuzz_targets/ruff_new_parser_equiv.rs index b8d6ead67b901c..082dee88574163 100644 --- a/fuzz/fuzz_targets/ruff_new_parser_equiv.rs +++ b/fuzz/fuzz_targets/ruff_new_parser_equiv.rs @@ -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