Skip to content

Commit

Permalink
Fix #597: Pop namespace scope after NsReader::read_to_end
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed May 2, 2023
1 parent 9fb797e commit a619fc1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@

### Bug Fixes

- [#597]: Fixed incorrect processing of namespace scopes in `NsReader::read_to_end`.
The scope started by a start element was not ended after that call.

### Misc Changes

[#597]: https://github.com/tafia/quick-xml/issues/597


## 0.28.2 -- 2023-04-12

Expand Down
6 changes: 5 additions & 1 deletion src/reader/ns_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,11 @@ impl<'i> NsReader<&'i [u8]> {
pub fn read_to_end(&mut self, end: QName) -> Result<Span> {
// According to the https://www.w3.org/TR/xml11/#dt-etag, end name should
// match literally the start name. See `Self::check_end_names` documentation
self.reader.read_to_end(end)
let result = self.reader.read_to_end(end)?;
// read_to_end will consume closing tag. Because nobody can access to its
// content anymore, we directly pop namespace of the opening tag
self.ns_resolver.pop(&mut self.buffer);
Ok(result)
}

/// Reads content between start and end tags, including any markup. This
Expand Down
37 changes: 35 additions & 2 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use std::sync::mpsc;

use quick_xml::events::{BytesStart, Event};
use quick_xml::name::QName;
use quick_xml::reader::Reader;
use quick_xml::name::{Namespace, QName, ResolveResult};
use quick_xml::reader::{NsReader, Reader};
use quick_xml::Error;

/// Regression test for https://github.com/tafia/quick-xml/issues/115
Expand Down Expand Up @@ -105,3 +105,36 @@ mod issue514 {
assert_eq!(reader.read_event().unwrap(), Event::Eof);
}
}

#[test]
fn issue597() {
const S: &'static str = r#"
<?xml version="1.0" encoding="UTF-8"?>
<oval_definitions xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<tests>
<xmlfilecontent_test xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
</xmlfilecontent_test>
<xmlfilecontent_test xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5#independent">
</xmlfilecontent_test>
</tests>
<objects/>
</oval_definitions>"#;

let mut reader = NsReader::from_str(S);
let ns = loop {
let (ns, ev) = reader.read_resolved_event().unwrap();
match ev {
Event::Start(v) if v.local_name().as_ref() == b"xmlfilecontent_test" => {
reader.read_to_end(v.name()).unwrap();
}
Event::Empty(v) if v.local_name().as_ref() == b"objects" => break ns,
_ => (),
}
};
assert_eq!(
ns,
ResolveResult::Bound(Namespace(
b"http://oval.mitre.org/XMLSchema/oval-definitions-5"
))
);
}

0 comments on commit a619fc1

Please sign in to comment.