Skip to content

Commit e87c7da

Browse files
manunioGeal
andauthoredNov 7, 2023
fuzz: fix fuzz build (#1707)
Co-authored-by: Geoffroy Couprie <contact@geoffroycouprie.com>
1 parent 69628bf commit e87c7da

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed
 

‎fuzz/fuzz_targets/fuzz_arithmetic.rs

+52-45
Original file line numberDiff line numberDiff line change
@@ -12,89 +12,96 @@ use nom::{
1212
combinator::{map, map_res, verify},
1313
multi::fold_many0,
1414
sequence::{delimited, pair, terminated},
15-
IResult,
15+
IResult, Parser
1616
};
1717

18-
use std::str::FromStr;
1918
use std::cell::RefCell;
19+
use std::str::FromStr;
2020

2121
thread_local! {
2222
pub static LEVEL: RefCell<u32> = RefCell::new(0);
2323
}
2424

2525
fn reset() {
26-
LEVEL.with(|l| {
27-
*l.borrow_mut() = 0;
28-
});
26+
LEVEL.with(|l| {
27+
*l.borrow_mut() = 0;
28+
});
2929
}
3030

3131
fn incr(i: &str) -> IResult<&str, ()> {
32-
LEVEL.with(|l| {
33-
*l.borrow_mut() += 1;
34-
35-
// limit the number of recursions, the fuzzer keeps running into them
36-
if *l.borrow() >= 8192 {
37-
return Err(nom::Err::Failure(nom::error::Error::new(i, nom::error::ErrorKind::Count)));
38-
} else {
39-
Ok((i, ()))
40-
}
41-
})
32+
LEVEL.with(|l| {
33+
*l.borrow_mut() += 1;
34+
35+
// limit the number of recursions, the fuzzer keeps running into them
36+
if *l.borrow() >= 8192 {
37+
return Err(nom::Err::Failure(nom::error::Error::new(
38+
i,
39+
nom::error::ErrorKind::Count,
40+
)));
41+
} else {
42+
Ok((i, ()))
43+
}
44+
})
4245
}
4346

4447
fn decr() {
45-
LEVEL.with(|l| {
46-
*l.borrow_mut() -= 1;
47-
});
48+
LEVEL.with(|l| {
49+
*l.borrow_mut() -= 1;
50+
});
4851
}
4952

5053
fn parens(i: &str) -> IResult<&str, i64> {
51-
delimited(space, delimited(
52-
terminated(tag("("), incr),
53-
expr,
54-
map(tag(")"), |_| decr())
55-
), space)(i)
54+
delimited(
55+
space,
56+
delimited(terminated(tag("("), incr), expr, map(tag(")"), |_| decr())),
57+
space,
58+
).parse(i)
5659
}
5760

58-
5961
fn factor(i: &str) -> IResult<&str, i64> {
6062
alt((
6163
map_res(delimited(space, digit, space), FromStr::from_str),
6264
parens,
63-
))(i)
65+
)).parse(i)
6466
}
6567

66-
6768
fn term(i: &str) -> IResult<&str, i64> {
6869
incr(i)?;
69-
let (i, init) = factor(i).map_err(|e| { decr(); e })?;
70+
let (i, init) = factor(i).map_err(|e| {
71+
decr();
72+
e
73+
})?;
7074

7175
let res = fold_many0(
7276
alt((
73-
pair(char('*'), factor),
74-
pair(char('/'), verify(factor, |i| *i != 0)),
77+
pair(char('*'), factor),
78+
pair(char('/'), verify(factor, |i| *i != 0)),
7579
)),
7680
|| init,
7781
|acc, (op, val): (char, i64)| {
7882
if op == '*' {
7983
acc.saturating_mul(val)
8084
} else {
8185
match acc.checked_div(val) {
82-
Some(v) => v,
83-
// we get a division with overflow because we can get acc = i64::MIN and val = -1
84-
// the division by zero is already checked earlier by verify
85-
None => i64::MAX,
86+
Some(v) => v,
87+
// we get a division with overflow because we can get acc = i64::MIN and val = -1
88+
// the division by zero is already checked earlier by verify
89+
None => i64::MAX,
8690
}
8791
}
8892
},
89-
)(i);
93+
).parse(i);
9094

9195
decr();
9296
res
9397
}
9498

9599
fn expr(i: &str) -> IResult<&str, i64> {
96100
incr(i)?;
97-
let (i, init) = term(i).map_err(|e| { decr(); e })?;
101+
let (i, init) = term(i).map_err(|e| {
102+
decr();
103+
e
104+
})?;
98105

99106
let res = fold_many0(
100107
pair(alt((char('+'), char('-'))), term),
@@ -106,20 +113,20 @@ fn expr(i: &str) -> IResult<&str, i64> {
106113
acc.saturating_sub(val)
107114
}
108115
},
109-
)(i);
116+
).parse(i);
110117

111118
decr();
112119
res
113120
}
114121

115122
fuzz_target!(|data: &[u8]| {
116-
reset();
117-
// fuzzed code goes here
118-
let temp = match str::from_utf8(data) {
119-
Ok(v) => {
120-
//println!("v: {}", v);
121-
factor(v)
122-
},
123-
Err(e) => factor("2"),
124-
};
123+
reset();
124+
// fuzzed code goes here
125+
let _ = match str::from_utf8(data) {
126+
Ok(v) => {
127+
//println!("v: {}", v);
128+
factor(v)
129+
}
130+
Err(_) => factor("2"),
131+
};
125132
});

0 commit comments

Comments
 (0)