From 25b2ed078779330c9ccca3fa058aa6fa8151572b Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sun, 29 Mar 2015 14:31:22 -0500 Subject: [PATCH] Fix operator bool() exception on zombie node --- include/yaml-cpp/node/impl.h | 5 +++-- test/integration/load_node_test.cpp | 4 ++-- test/node/node_test.cpp | 7 +++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h index 092351e..164c533 100644 --- a/include/yaml-cpp/node/impl.h +++ b/include/yaml-cpp/node/impl.h @@ -60,8 +60,9 @@ inline void Node::EnsureNodeExists() const { } inline bool Node::IsDefined() const { - if (!m_isValid) - throw InvalidNode(); + if (!m_isValid) { + return false; + } return m_pNode ? m_pNode->is_defined() : true; } diff --git a/test/integration/load_node_test.cpp b/test/integration/load_node_test.cpp index b2288ed..80236af 100644 --- a/test/integration/load_node_test.cpp +++ b/test/integration/load_node_test.cpp @@ -181,8 +181,8 @@ TEST(LoadNodeTest, DereferenceIteratorError) { EXPECT_THROW(node.begin()->first.as(), InvalidNode); EXPECT_EQ(true, (*node.begin()).IsMap()); EXPECT_EQ(true, node.begin()->IsMap()); - EXPECT_THROW((*node.begin()->begin()).IsDefined(), InvalidNode); - EXPECT_THROW(node.begin()->begin()->IsDefined(), InvalidNode); + EXPECT_THROW((*node.begin()->begin()).Type(), InvalidNode); + EXPECT_THROW(node.begin()->begin()->Type(), InvalidNode); } TEST(NodeTest, EmitEmptyNode) { diff --git a/test/node/node_test.cpp b/test/node/node_test.cpp index 0aecb66..f54fcc9 100644 --- a/test/node/node_test.cpp +++ b/test/node/node_test.cpp @@ -281,6 +281,13 @@ TEST(NodeTest, DefaultNodeStyle) { EXPECT_EQ(EmitterStyle::Default, node.Style()); } +TEST(NodeTest, AccessNonexistentKeyOnConstNode) { + YAML::Node node; + node["3"] = "4"; + const YAML::Node& other = node; + ASSERT_FALSE(other["5"]); +} + class NodeEmitterTest : public ::testing::Test { protected: void ExpectOutput(const std::string& output, const Node& node) {