From 76ecfff7b692620b726bd61878b603420fd9b1e6 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Thu, 19 Jun 2014 17:17:33 +0200 Subject: [PATCH] os: Handle corner case in Xml_node::sub_node When calling 'sub_node' on a node with no sub nodes, the Xml_node would interpret the characters after the current node while searching for sub nodes. The patch adds a sanity check that lets the 'sub_node' function throw an exception when called on a node with no sub nodes. --- repos/os/include/util/xml_node.h | 34 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/repos/os/include/util/xml_node.h b/repos/os/include/util/xml_node.h index b62eecb93f..cfc338c09f 100644 --- a/repos/os/include/util/xml_node.h +++ b/repos/os/include/util/xml_node.h @@ -621,13 +621,16 @@ namespace Genode { */ Xml_node sub_node(unsigned idx = 0U) const { - /* look up node at specified index */ - try { - Xml_node curr_node = _sub_node(content_addr()); - for (; idx > 0; idx--) - curr_node = curr_node.next(); - return curr_node; - } catch (Invalid_syntax) { } + if (_num_sub_nodes > 0) { + + /* look up node at specified index */ + try { + Xml_node curr_node = _sub_node(content_addr()); + for (; idx > 0; idx--) + curr_node = curr_node.next(); + return curr_node; + } catch (Invalid_syntax) { } + } throw Nonexistent_sub_node(); } @@ -639,13 +642,16 @@ namespace Genode { */ Xml_node sub_node(const char *type) const { - /* search for sub node of specified type */ - try { - Xml_node curr_node = _sub_node(content_addr()); - for ( ; true; curr_node = curr_node.next()) - if (curr_node.has_type(type)) - return curr_node; - } catch (...) { } + if (_num_sub_nodes > 0) { + + /* search for sub node of specified type */ + try { + Xml_node curr_node = _sub_node(content_addr()); + for ( ; true; curr_node = curr_node.next()) + if (curr_node.has_type(type)) + return curr_node; + } catch (...) { } + } throw Nonexistent_sub_node(); }