From e0438f9b887108e4dc437b83f9c81904f7a2b32a Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sat, 12 May 2018 17:59:04 +0200 Subject: [PATCH] jingle_message, muc, presence, version: Always use the check_no_attributes macro. --- src/jingle_message.rs | 6 +----- src/muc/muc.rs | 5 +---- src/muc/user.rs | 17 +++-------------- src/presence.rs | 14 +++----------- src/version.rs | 4 +--- 5 files changed, 9 insertions(+), 37 deletions(-) diff --git a/src/jingle_message.rs b/src/jingle_message.rs index 779af992..159ce597 100644 --- a/src/jingle_message.rs +++ b/src/jingle_message.rs @@ -28,11 +28,7 @@ pub enum JingleMI { } fn get_sid(elem: Element) -> Result { - for (attr, _) in elem.attrs() { - if attr != "id" { - return Err(Error::ParseError("Unknown attribute in Jingle message element.")); - } - } + check_no_unknown_attributes!(elem, "Jingle message", ["id"]); Ok(SessionId(get_attr!(elem, "id", required))) } diff --git a/src/muc/muc.rs b/src/muc/muc.rs index 550cc61e..9758b800 100644 --- a/src/muc/muc.rs +++ b/src/muc/muc.rs @@ -25,6 +25,7 @@ impl TryFrom for Muc { if !elem.is("x", ns::MUC) { return Err(Error::ParseError("This is not an x element.")); } + check_no_attributes!(elem, "x"); let mut password = None; for child in elem.children() { @@ -35,10 +36,6 @@ impl TryFrom for Muc { } } - for _ in elem.attrs() { - return Err(Error::ParseError("Unknown attribute in x element.")); - } - Ok(Muc { password: password, }) diff --git a/src/muc/user.rs b/src/muc/user.rs index d09c73c4..236ca8c8 100644 --- a/src/muc/user.rs +++ b/src/muc/user.rs @@ -96,14 +96,10 @@ impl TryFrom for Actor { if !elem.is("actor", ns::MUC_USER) { return Err(Error::ParseError("This is not a actor element.")); } + check_no_unknown_attributes!(elem, "actor", ["jid", "nick"]); for _ in elem.children() { return Err(Error::ParseError("Unknown child in actor element.")); } - for (attr, _) in elem.attrs() { - if attr != "jid" && attr != "nick" { - return Err(Error::ParseError("Unknown attribute in actor element.")); - } - } let jid: Option = get_attr!(elem, "jid", optional); let nick = get_attr!(elem, "nick", optional); @@ -167,6 +163,7 @@ impl TryFrom for Item { if !elem.is("item", ns::MUC_USER) { return Err(Error::ParseError("This is not a item element.")); } + check_no_unknown_attributes!(elem, "item", ["affiliation", "jid", "nick", "role"]); let mut actor: Option = None; let mut continue_: Option = None; let mut reason: Option = None; @@ -181,12 +178,6 @@ impl TryFrom for Item { return Err(Error::ParseError("Unknown child in item element.")); } } - for (attr, _) in elem.attrs() { - if attr != "affiliation" && attr != "jid" && - attr != "nick" && attr != "role" { - return Err(Error::ParseError("Unknown attribute in item element.")); - } - } let affiliation: Affiliation = get_attr!(elem, "affiliation", required); let jid: Option = get_attr!(elem, "jid", optional); @@ -233,6 +224,7 @@ impl TryFrom for MucUser { if !elem.is("x", ns::MUC_USER) { return Err(Error::ParseError("This is not an x element.")); } + check_no_attributes!(elem, "x"); let mut status = vec!(); let mut items = vec!(); for child in elem.children() { @@ -244,9 +236,6 @@ impl TryFrom for MucUser { return Err(Error::ParseError("Unknown child in x element.")); } } - for _ in elem.attrs() { - return Err(Error::ParseError("Unknown attribute in x element.")); - } Ok(MucUser { status, items, diff --git a/src/presence.rs b/src/presence.rs index e48e6850..d25a7b09 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -268,22 +268,16 @@ impl TryFrom for Presence { if show.is_some() { return Err(Error::ParseError("More than one show element in a presence.")); } + check_no_attributes!(elem, "show"); for _ in elem.children() { return Err(Error::ParseError("Unknown child in show element.")); } - for _ in elem.attrs() { - return Err(Error::ParseError("Unknown attribute in show element.")); - } show = Some(Show::from_str(elem.text().as_ref())?); } else if elem.is("status", ns::DEFAULT_NS) { + check_no_unknown_attributes!(elem, "status", ["xml:lang"]); for _ in elem.children() { return Err(Error::ParseError("Unknown child in status element.")); } - for (attr, _) in elem.attrs() { - if attr != "xml:lang" { - return Err(Error::ParseError("Unknown attribute in status element.")); - } - } let lang = get_attr!(elem, "xml:lang", default); if presence.statuses.insert(lang, elem.text()).is_some() { return Err(Error::ParseError("Status element present twice for the same xml:lang.")); @@ -292,12 +286,10 @@ impl TryFrom for Presence { if priority.is_some() { return Err(Error::ParseError("More than one priority element in a presence.")); } + check_no_attributes!(elem, "status"); for _ in elem.children() { return Err(Error::ParseError("Unknown child in priority element.")); } - for _ in elem.attrs() { - return Err(Error::ParseError("Unknown attribute in priority element.")); - } priority = Some(Priority::from_str(elem.text().as_ref())?); } else { presence.payloads.push(elem.clone()); diff --git a/src/version.rs b/src/version.rs index 5e7f0abf..e14bb422 100644 --- a/src/version.rs +++ b/src/version.rs @@ -23,9 +23,7 @@ impl TryFrom for Version { if !elem.is("query", ns::VERSION) { return Err(Error::ParseError("This is not a version element.")); } - for _ in elem.attrs() { - return Err(Error::ParseError("Unknown child in version element.")); - } + check_no_attributes!(elem, "version"); let mut name = None; let mut version = None; let mut os = None;