|
| 1 | +#![no_main] |
| 2 | + |
| 3 | +use { |
| 4 | + libfuzzer_sys::{fuzz_target, Corpus}, |
| 5 | + regex_syntax::ast::{ |
| 6 | + parse::Parser, visit, Ast, Flag, Group, GroupKind, SetFlags, Visitor, |
| 7 | + }, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Eq, PartialEq, arbitrary::Arbitrary)] |
| 11 | +struct FuzzData { |
| 12 | + ast: Ast, |
| 13 | +} |
| 14 | + |
| 15 | +impl std::fmt::Debug for FuzzData { |
| 16 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 17 | + let mut builder = f.debug_struct("FuzzData"); |
| 18 | + builder.field("ast", &format!("{}", self.ast)); |
| 19 | + builder.finish() |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +struct VerboseVisitor; |
| 24 | + |
| 25 | +impl Visitor for VerboseVisitor { |
| 26 | + type Output = (); |
| 27 | + type Err = (); |
| 28 | + |
| 29 | + fn finish(self) -> Result<Self::Output, Self::Err> { |
| 30 | + Ok(()) |
| 31 | + } |
| 32 | + |
| 33 | + fn visit_pre(&mut self, ast: &Ast) -> Result<Self::Output, Self::Err> { |
| 34 | + match ast { |
| 35 | + Ast::Flags(SetFlags { flags, .. }) |
| 36 | + | Ast::Group(Group { |
| 37 | + kind: GroupKind::NonCapturing(flags), .. |
| 38 | + }) if flags |
| 39 | + .flag_state(Flag::IgnoreWhitespace) |
| 40 | + .unwrap_or(false) => |
| 41 | + { |
| 42 | + Err(()) |
| 43 | + } |
| 44 | + _ => Ok(()), |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +fuzz_target!(|data: FuzzData| -> Corpus { |
| 50 | + let pattern = format!("{}", data.ast); |
| 51 | + let Ok(ast) = Parser::new().parse(&pattern) else { |
| 52 | + return Corpus::Keep; |
| 53 | + }; |
| 54 | + if visit(&ast, VerboseVisitor).is_err() { |
| 55 | + return Corpus::Reject; |
| 56 | + } |
| 57 | + let ast2 = Parser::new().parse(&ast.to_string()).unwrap(); |
| 58 | + assert_eq!( |
| 59 | + ast, |
| 60 | + ast2, |
| 61 | + "Found difference:\ |
| 62 | + \nleft: {:?}\ |
| 63 | + \nright: {:?}\ |
| 64 | + \nIf these two match, then there was a parsing difference; \ |
| 65 | + maybe non-determinism?", |
| 66 | + ast.to_string(), |
| 67 | + ast2.to_string() |
| 68 | + ); |
| 69 | + Corpus::Keep |
| 70 | +}); |
0 commit comments