Skip to content

Commit

Permalink
Avoids crash on invalid comments when the buffer when the buffer was …
Browse files Browse the repository at this point in the history
…not cleared before

Closes tafia#604
  • Loading branch information
Tpt committed Jun 27, 2023
1 parent 60249ae commit c6a99f7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 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 `<!-->` when the buffer was not cleared before.

### 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
2 changes: 1 addition & 1 deletion src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ 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) {
if let Some((consumed, used)) = bang_type.parse(&buf[start..], available) {
buf.extend_from_slice(consumed);

self $(.$reader)? .consume(used);
Expand Down
73 changes: 72 additions & 1 deletion 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, Event};
use quick_xml::name::QName;
use quick_xml::reader::Reader;
use quick_xml::Error;
Expand Down Expand Up @@ -105,3 +105,74 @@ mod issue514 {
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;
use quick_xml::events::BytesText;

#[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))
);
assert!(matches!(
reader.read_event_into(&mut buf),
Err(Error::UnexpectedEof(_))
));
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))
);
assert!(matches!(
reader.read_event_into(&mut buf),
Err(Error::UnexpectedEof(_))
));
assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Eof);
}

#[test]
fn small_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);
}

#[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 c6a99f7

Please sign in to comment.