afl: add target parse_balance
verify all containers are balanced
This commit is contained in:
parent
88a872cd88
commit
3c17d6df49
5 changed files with 24 additions and 0 deletions
1
.github/workflows/ci.yml
vendored
1
.github/workflows/ci.yml
vendored
|
@ -72,6 +72,7 @@ jobs:
|
|||
matrix:
|
||||
target:
|
||||
- parse
|
||||
- parse_balance
|
||||
- html
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
|
|
@ -17,6 +17,10 @@ path = "src/main.rs"
|
|||
name = "parse"
|
||||
path = "src/parse.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "parse_balance"
|
||||
path = "src/parse_balance.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "html"
|
||||
path = "src/html.rs"
|
||||
|
|
|
@ -11,6 +11,21 @@ pub fn parse(data: &[u8]) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Ensure containers are always balanced, i.e. opened and closed in correct order.
|
||||
pub fn parse_balance(data: &[u8]) {
|
||||
if let Ok(s) = std::str::from_utf8(data) {
|
||||
let mut open = Vec::new();
|
||||
for event in jotdown::Parser::new(s) {
|
||||
match event {
|
||||
jotdown::Event::Start(c, ..) => open.push(c.clone()),
|
||||
jotdown::Event::End(c) => assert_eq!(open.pop().unwrap(), c),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
assert_eq!(open, &[]);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn html(data: &[u8]) {
|
||||
if data.iter().any(|i| *i == 0) {
|
||||
return;
|
||||
|
|
|
@ -8,6 +8,7 @@ fn main() {
|
|||
|
||||
let f = match target.as_str() {
|
||||
"parse" => jotdown_afl::parse,
|
||||
"parse_balance" => jotdown_afl::parse_balance,
|
||||
"html" => jotdown_afl::html,
|
||||
_ => panic!("unknown target '{}'", target),
|
||||
};
|
||||
|
|
3
tests/afl/src/parse_balance.rs
Normal file
3
tests/afl/src/parse_balance.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
afl::fuzz!(|data: &[u8]| { jotdown_afl::parse_balance(data) });
|
||||
}
|
Loading…
Reference in a new issue