Skip to content

Commit

Permalink
Make sure parse_all stops on Errors (#472)
Browse files Browse the repository at this point in the history
All uses of `Parser::parse_all` in the code immediately propagate errors
and thus stop iteration. However the Iterator interface does not mandate
this, and not handling the inner `Result` at all can way too easily lead
to infinite loops.
  • Loading branch information
Swatinem authored Feb 7, 2022
1 parent 43860b8 commit 4fce4f1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crates/wasmparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,10 @@ impl Parser {
}
let payload = match cur.parse(data, true) {
// Propagate all errors
Err(e) => return Some(Err(e)),
Err(e) => {
done = true;
return Some(Err(e));
}

// This isn't possible because `eof` is always true.
Ok(Chunk::NeedMoreData(_)) => unreachable!(),
Expand Down Expand Up @@ -997,6 +1000,14 @@ mod tests {
);
}

#[test]
fn header_iter() {
for _ in Parser::default().parse_all(&[]) {}
for _ in Parser::default().parse_all(b"\0") {}
for _ in Parser::default().parse_all(b"\0asm") {}
for _ in Parser::default().parse_all(b"\0asm\x01\x01\x01\x01") {}
}

fn parser_after_header() -> Parser {
let mut p = Parser::default();
assert_matches!(
Expand Down

0 comments on commit 4fce4f1

Please sign in to comment.