From e87c7da9fa2ddb943306369f6c6d9e914256edbc Mon Sep 17 00:00:00 2001 From: manunio Date: Wed, 8 Nov 2023 02:08:06 +0530 Subject: [PATCH] fuzz: fix fuzz build (#1707) Co-authored-by: Geoffroy Couprie --- fuzz/fuzz_targets/fuzz_arithmetic.rs | 97 +++++++++++++++------------- 1 file changed, 52 insertions(+), 45 deletions(-) diff --git a/fuzz/fuzz_targets/fuzz_arithmetic.rs b/fuzz/fuzz_targets/fuzz_arithmetic.rs index 4b105d24c..7b10a3fc9 100644 --- a/fuzz/fuzz_targets/fuzz_arithmetic.rs +++ b/fuzz/fuzz_targets/fuzz_arithmetic.rs @@ -12,66 +12,70 @@ use nom::{ combinator::{map, map_res, verify}, multi::fold_many0, sequence::{delimited, pair, terminated}, - IResult, + IResult, Parser }; -use std::str::FromStr; use std::cell::RefCell; +use std::str::FromStr; thread_local! { pub static LEVEL: RefCell = RefCell::new(0); } fn reset() { - LEVEL.with(|l| { - *l.borrow_mut() = 0; - }); + LEVEL.with(|l| { + *l.borrow_mut() = 0; + }); } fn incr(i: &str) -> IResult<&str, ()> { - LEVEL.with(|l| { - *l.borrow_mut() += 1; - - // limit the number of recursions, the fuzzer keeps running into them - if *l.borrow() >= 8192 { - return Err(nom::Err::Failure(nom::error::Error::new(i, nom::error::ErrorKind::Count))); - } else { - Ok((i, ())) - } - }) + LEVEL.with(|l| { + *l.borrow_mut() += 1; + + // limit the number of recursions, the fuzzer keeps running into them + if *l.borrow() >= 8192 { + return Err(nom::Err::Failure(nom::error::Error::new( + i, + nom::error::ErrorKind::Count, + ))); + } else { + Ok((i, ())) + } + }) } fn decr() { - LEVEL.with(|l| { - *l.borrow_mut() -= 1; - }); + LEVEL.with(|l| { + *l.borrow_mut() -= 1; + }); } fn parens(i: &str) -> IResult<&str, i64> { - delimited(space, delimited( - terminated(tag("("), incr), - expr, - map(tag(")"), |_| decr()) - ), space)(i) + delimited( + space, + delimited(terminated(tag("("), incr), expr, map(tag(")"), |_| decr())), + space, + ).parse(i) } - fn factor(i: &str) -> IResult<&str, i64> { alt(( map_res(delimited(space, digit, space), FromStr::from_str), parens, - ))(i) + )).parse(i) } - fn term(i: &str) -> IResult<&str, i64> { incr(i)?; - let (i, init) = factor(i).map_err(|e| { decr(); e })?; + let (i, init) = factor(i).map_err(|e| { + decr(); + e + })?; let res = fold_many0( alt(( - pair(char('*'), factor), - pair(char('/'), verify(factor, |i| *i != 0)), + pair(char('*'), factor), + pair(char('/'), verify(factor, |i| *i != 0)), )), || init, |acc, (op, val): (char, i64)| { @@ -79,14 +83,14 @@ fn term(i: &str) -> IResult<&str, i64> { acc.saturating_mul(val) } else { match acc.checked_div(val) { - Some(v) => v, - // we get a division with overflow because we can get acc = i64::MIN and val = -1 - // the division by zero is already checked earlier by verify - None => i64::MAX, + Some(v) => v, + // we get a division with overflow because we can get acc = i64::MIN and val = -1 + // the division by zero is already checked earlier by verify + None => i64::MAX, } } }, - )(i); + ).parse(i); decr(); res @@ -94,7 +98,10 @@ fn term(i: &str) -> IResult<&str, i64> { fn expr(i: &str) -> IResult<&str, i64> { incr(i)?; - let (i, init) = term(i).map_err(|e| { decr(); e })?; + let (i, init) = term(i).map_err(|e| { + decr(); + e + })?; let res = fold_many0( pair(alt((char('+'), char('-'))), term), @@ -106,20 +113,20 @@ fn expr(i: &str) -> IResult<&str, i64> { acc.saturating_sub(val) } }, - )(i); + ).parse(i); decr(); res } fuzz_target!(|data: &[u8]| { - reset(); - // fuzzed code goes here - let temp = match str::from_utf8(data) { - Ok(v) => { - //println!("v: {}", v); - factor(v) - }, - Err(e) => factor("2"), - }; + reset(); + // fuzzed code goes here + let _ = match str::from_utf8(data) { + Ok(v) => { + //println!("v: {}", v); + factor(v) + } + Err(_) => factor("2"), + }; });