Skip to content

Commit

Permalink
Fails on empty doctype
Browse files Browse the repository at this point in the history
The parser was crashing with debug assertions enabled but passing with debug assertions disabled

Closes tafia#608
  • Loading branch information
Tpt committed Jun 11, 2023
1 parent 84b07b4 commit 0de827c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/errors.rs
Expand Up @@ -37,6 +37,8 @@ pub enum Error {
/// `Event::BytesDecl` must start with *version* attribute. Contains the attribute
/// that was found or `None` if an xml declaration doesn't contain attributes.
XmlDeclWithoutVersion(Option<String>),
/// Empty `Event::DocType`.
EmptyDocType,
/// Attribute parsing error
InvalidAttr(AttrError),
/// Escape error
Expand Down Expand Up @@ -109,6 +111,7 @@ impl fmt::Display for Error {
"XmlDecl must start with 'version' attribute, found {:?}",
e
),
Error::EmptyDocType => write!(f, "DOCTYPE declaration must not be empty"),
Error::InvalidAttr(e) => write!(f, "error while parsing attribute: {}", e),
Error::EscapeError(e) => write!(f, "{}", e),
Error::UnknownPrefix(prefix) => {
Expand Down
4 changes: 3 additions & 1 deletion src/reader/parser.rs
Expand Up @@ -117,7 +117,9 @@ impl Parser {
.iter()
.position(|b| !is_whitespace(*b))
.unwrap_or(len - 8);
debug_assert!(start < len - 8, "DocType must have a name");
if start + 8 >= len {
return Err(Error::EmptyDocType);
}
Ok(Event::DocType(BytesText::wrap(
&buf[8 + start..],
self.decoder(),
Expand Down
10 changes: 10 additions & 0 deletions tests/fuzzing.rs
Expand Up @@ -3,6 +3,7 @@
use quick_xml::events::Event;
use quick_xml::reader::Reader;
use std::io::Cursor;
use quick_xml::Error;

#[test]
fn fuzz_53() {
Expand Down Expand Up @@ -50,3 +51,12 @@ fn fuzz_101() {
buf.clear();
}
}

#[test]
fn fuzz_empty_doctype() {
let data = b"<!doctype>";
let mut reader = Reader::from_reader(data.as_slice());
let mut buf = Vec::new();
assert!(matches!(reader.read_event_into(&mut buf).unwrap_err(), Error::EmptyDocType));
assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Eof);
}

0 comments on commit 0de827c

Please sign in to comment.