Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails on empty comments without 4 -" #618

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to understand, how the fix worked. I feel it will be worth if this line will have a comment, why we need to take bytes only from start. Maybe with an examples of buf content for better understanding. If you known, what happens here, could you add such a comment? If not, I'll investigate that myself. Because tests are passed I assume that the fix is correct, but I want to know why it is correct

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, I understood. We should parse only newly read data from the buf. This is correct, because we in the function that parses the whole comment, so any data that exists in the buffer before is not relevant here and should be skipped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. I have added the comment. Not doing so was making some checks like the one on this line to be incorrect.

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);
}
}