Skip to content

Commit

Permalink
Merge pull request #618 from Tpt/issue-604
Browse files Browse the repository at this point in the history
Fails on empty comments without 4 -"
  • Loading branch information
Mingun authored Jun 28, 2023
2 parents 60249ae + 5b8c1aa commit aca1f79
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

### Bug Fixes

- [#604]: Avoid crashing on wrong comments like `<!-->` when using `read_event_into*` functions.

### Misc Changes


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

Expand Down
4 changes: 3 additions & 1 deletion src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ macro_rules! impl_buffered_source {
// somewhere sane rather than at the EOF
Ok(n) if n.is_empty() => return Err(bang_type.to_err()),
Ok(available) => {
if let Some((consumed, used)) = bang_type.parse(buf, available) {
// We only parse from start because we don't want to consider
// whatever is in the buffer before the bang element
if let Some((consumed, used)) = bang_type.parse(&buf[start..], available) {
buf.extend_from_slice(consumed);

self $(.$reader)? .consume(used);
Expand Down
84 changes: 82 additions & 2 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::sync::mpsc;

use quick_xml::events::{BytesStart, Event};
use quick_xml::events::{BytesDecl, BytesStart, BytesText, Event};
use quick_xml::name::QName;
use quick_xml::reader::Reader;
use quick_xml::Error;
Expand Down Expand Up @@ -98,10 +98,90 @@ mod issue514 {
assert_eq!(found, "other-tag");
}
x => panic!(
r#"Expected `Err(EndEventMismatch("some-tag", "other-tag")))`, but found {:?}"#,
r#"Expected `Err(EndEventMismatch("some-tag", "other-tag"))`, but found {:?}"#,
x
),
}
assert_eq!(reader.read_event().unwrap(), Event::Eof);
}
}

/// Regression test for https://github.com/tafia/quick-xml/issues/604
mod issue604 {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn short() {
let data = b"<?xml version=\"1.0\"?><!-->";
let mut reader = Reader::from_reader(data.as_slice());
let mut buf = Vec::new();
assert_eq!(
reader.read_event_into(&mut buf).unwrap(),
Event::Decl(BytesDecl::new("1.0", None, None))
);
match reader.read_event_into(&mut buf) {
Err(Error::UnexpectedEof(reason)) => assert_eq!(reason, "Comment"),
x => panic!(
r#"Expected `Err(UnexpectedEof("Comment"))`, but found {:?}"#,
x
),
}
assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Eof);
}

#[test]
fn long() {
let data = b"<?xml version=\"1.0\"?><!--->";
let mut reader = Reader::from_reader(data.as_slice());
let mut buf = Vec::new();
assert_eq!(
reader.read_event_into(&mut buf).unwrap(),
Event::Decl(BytesDecl::new("1.0", None, None))
);
match reader.read_event_into(&mut buf) {
Err(Error::UnexpectedEof(reason)) => assert_eq!(reason, "Comment"),
x => panic!(
r#"Expected `Err(UnexpectedEof("Comment"))`, but found {:?}"#,
x
),
}
assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Eof);
}

/// According to the grammar, `>` is allowed just in start of comment.
/// See https://www.w3.org/TR/xml11/#sec-comments
#[test]
fn short_valid() {
let data = b"<?xml version=\"1.0\"?><!-->-->";
let mut reader = Reader::from_reader(data.as_slice());
let mut buf = Vec::new();
assert_eq!(
reader.read_event_into(&mut buf).unwrap(),
Event::Decl(BytesDecl::new("1.0", None, None))
);
assert_eq!(
reader.read_event_into(&mut buf).unwrap(),
Event::Comment(BytesText::from_escaped(">"))
);
assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Eof);
}

/// According to the grammar, `->` is allowed just in start of comment.
/// See https://www.w3.org/TR/xml11/#sec-comments
#[test]
fn long_valid() {
let data = b"<?xml version=\"1.0\"?><!--->-->";
let mut reader = Reader::from_reader(data.as_slice());
let mut buf = Vec::new();
assert_eq!(
reader.read_event_into(&mut buf).unwrap(),
Event::Decl(BytesDecl::new("1.0", None, None))
);
assert_eq!(
reader.read_event_into(&mut buf).unwrap(),
Event::Comment(BytesText::from_escaped("->"))
);
assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Eof);
}
}

0 comments on commit aca1f79

Please sign in to comment.