afl: add target parse_balance

verify all containers are balanced
This commit is contained in:
Noah Hellman 2023-04-10 13:23:44 +02:00
parent 88a872cd88
commit 3c17d6df49
5 changed files with 24 additions and 0 deletions

View file

@ -72,6 +72,7 @@ jobs:
matrix: matrix:
target: target:
- parse - parse
- parse_balance
- html - html
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View file

@ -17,6 +17,10 @@ path = "src/main.rs"
name = "parse" name = "parse"
path = "src/parse.rs" path = "src/parse.rs"
[[bin]]
name = "parse_balance"
path = "src/parse_balance.rs"
[[bin]] [[bin]]
name = "html" name = "html"
path = "src/html.rs" path = "src/html.rs"

View file

@ -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]) { pub fn html(data: &[u8]) {
if data.iter().any(|i| *i == 0) { if data.iter().any(|i| *i == 0) {
return; return;

View file

@ -8,6 +8,7 @@ fn main() {
let f = match target.as_str() { let f = match target.as_str() {
"parse" => jotdown_afl::parse, "parse" => jotdown_afl::parse,
"parse_balance" => jotdown_afl::parse_balance,
"html" => jotdown_afl::html, "html" => jotdown_afl::html,
_ => panic!("unknown target '{}'", target), _ => panic!("unknown target '{}'", target),
}; };

View file

@ -0,0 +1,3 @@
fn main() {
afl::fuzz!(|data: &[u8]| { jotdown_afl::parse_balance(data) });
}