Skip to content

Commit

Permalink
Fails on empty comments without 4 -"
Browse files Browse the repository at this point in the history
The parser was crashing because of bad slice bounds

Closes tafia#604
  • Loading branch information
Tpt committed Jun 25, 2023
1 parent 60249ae commit 2b152be
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

### Bug Fixes

### Misc Changes
- [#618]: Avoid crashing on wrong comments `<!-->`.

### Misc Changes

[#609]: https://github.com/tafia/quick-xml/pull/609
[#615]: https://github.com/tafia/quick-xml/pull/615

[#618]: https://github.com/tafia/quick-xml/pull/618

## 0.29.0 -- 2023-06-13

Expand Down
4 changes: 4 additions & 0 deletions src/reader/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ impl Parser {
return Err(Error::UnexpectedToken("--".to_string()));
}
}
if len < 5 {
// We do not have any text content
return Err(Error::UnexpectedToken("<!-->".to_string()));
}
Ok(Event::Comment(BytesText::wrap(
&buf[3..len - 2],
self.decoder(),
Expand Down
26 changes: 26 additions & 0 deletions tests/fuzzing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,29 @@ fn fuzz_empty_doctype() {
));
assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Eof);
}

#[test]
fn fuzz_empty_comment() {
let data = b"<?xml version=\"1.0\" encoding=\"utf-8\"?><!-->";
let mut reader = Reader::from_reader(data.as_slice());
let mut buf = Vec::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Eof) => break,
_ => (),
}
}
}

#[test]
fn fuzz_empty_comment2() {
let data = b"<?xml version=\"1.0\" encoding=\"utf-8\"?><!--->";
let mut reader = Reader::from_reader(data.as_slice());
let mut buf = Vec::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Eof) => break,
_ => (),
}
}
}

0 comments on commit 2b152be

Please sign in to comment.