Skip to content

Commit

Permalink
avm2: Improve quick_xml to AVM2 XML error conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-McSweeney authored and Lord-McSweeney committed Oct 21, 2023
1 parent 4de186e commit 5f217a6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
66 changes: 52 additions & 14 deletions core/src/avm2/e4x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::{

use gc_arena::{Collect, GcCell, Mutation};
use quick_xml::{
events::{BytesStart, Event},
events::{attributes::AttrError as XmlAttrError, BytesStart, Event},
name::ResolveResult,
NsReader,
Error as XmlError, NsReader,
};

use crate::{avm2::TObject, xml::custom_unescape};
Expand Down Expand Up @@ -51,15 +51,51 @@ impl<'gc> Debug for E4XNodeData<'gc> {
}
}

fn malformed_element<'gc>(activation: &mut Activation<'_, 'gc>) -> Error<'gc> {
Error::AvmError(
type_error(
fn make_xml_error<'gc>(activation: &mut Activation<'_, 'gc>, err: XmlError) -> Error<'gc> {
let error = match err {
XmlError::InvalidAttr(XmlAttrError::Duplicated(_, _)) => type_error(
activation,
"Error #1104: Attribute was already specified for element.",
1104,
),
XmlError::UnexpectedEof(currently_parsing) => match currently_parsing.as_str() {
"CData" => type_error(
activation,
"Error #1091: XML parser failure: Unterminated CDATA section.",
1091,
),
"DOCTYPE" => type_error(
activation,
"Error #1093: XML parser failure: Unterminated DOCTYPE declaration.",
1093,
),
"Comment" => type_error(
activation,
"Error #1094: XML parser failure: Unterminated comment.",
1094,
),
"XmlDecl" => type_error(
activation,
"Error #1097: XML parser failure: Unterminated processing instruction.",
1097,
),
_ => type_error(
activation,
"Error #1090: XML parser failure: element is malformed.",
1090,
),
},
_ => type_error(
activation,
"Error #1090: XML parser failure: element is malformed.",
1090,
)
.expect("Failed to construct XML TypeError"),
)
),
};

match error {
Ok(err) => Error::AvmError(err),
Err(err) => err,
}
}

#[derive(Collect, Debug)]
Expand Down Expand Up @@ -607,7 +643,7 @@ impl<'gc> E4XNode<'gc> {
loop {
let event = parser
.read_event()
.map_err(|_| malformed_element(activation))?;
.map_err(|e| make_xml_error(activation, e))?;

match &event {
Event::Start(bs) => {
Expand All @@ -633,7 +669,7 @@ impl<'gc> E4XNode<'gc> {
Event::Text(bt) => {
handle_text_cdata(
custom_unescape(bt, parser.decoder())
.map_err(|_| malformed_element(activation))?
.map_err(|e| make_xml_error(activation, e))?
.as_bytes(),
ignore_white,
&mut open_tags,
Expand All @@ -659,7 +695,7 @@ impl<'gc> E4XNode<'gc> {
continue;
}
let text = custom_unescape(bt, parser.decoder())
.map_err(|_| malformed_element(activation))?;
.map_err(|e| make_xml_error(activation, e))?;
let text =
AvmString::new_utf8_bytes(activation.context.gc_context, text.as_bytes());
let node = E4XNode(GcCell::new(
Expand All @@ -679,7 +715,7 @@ impl<'gc> E4XNode<'gc> {
continue;
}
let text = custom_unescape(bt, parser.decoder())
.map_err(|_| malformed_element(activation))?;
.map_err(|e| make_xml_error(activation, e))?;
let (name, value) = if let Some((name, value)) = text.split_once(' ') {
(
AvmString::new_utf8_bytes(
Expand Down Expand Up @@ -733,7 +769,9 @@ impl<'gc> E4XNode<'gc> {
let mut attribute_nodes = Vec::new();

let attributes: Result<Vec<_>, _> = bs.attributes().collect();
for attribute in attributes.map_err(|_| malformed_element(activation))? {
for attribute in
attributes.map_err(|e| make_xml_error(activation, XmlError::InvalidAttr(e)))?
{
let (ns, local_name) = parser.resolve_attribute(attribute.key);
let name =
AvmString::new_utf8_bytes(activation.context.gc_context, local_name.into_inner());
Expand Down Expand Up @@ -763,7 +801,7 @@ impl<'gc> E4XNode<'gc> {
};

let value_str = custom_unescape(&attribute.value, decoder)
.map_err(|_| malformed_element(activation))?;
.map_err(|e| make_xml_error(activation, e))?;
let value =
AvmString::new_utf8_bytes(activation.context.gc_context, value_str.as_bytes());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
num_ticks = 1
known_failure = true

0 comments on commit 5f217a6

Please sign in to comment.