diff --git a/docs/Breaking-Changes.md b/docs/Breaking-Changes.md index 85103cd..3b23127 100644 --- a/docs/Breaking-Changes.md +++ b/docs/Breaking-Changes.md @@ -4,7 +4,7 @@ ## HEAD ## -_none_ + * Throws an exception when trying to parse a negative number as an unsigned integer. ## 0.6.0 ## diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index d6bd365..db7e44d 100644 --- a/include/yaml-cpp/node/convert.h +++ b/include/yaml-cpp/node/convert.h @@ -15,6 +15,7 @@ #include #include #include +#include #include "yaml-cpp/binary.h" #include "yaml-cpp/node/impl.h" @@ -133,6 +134,9 @@ inner_encode(const T& rhs, std::stringstream& stream){ const std::string& input = node.Scalar(); \ std::stringstream stream(input); \ stream.unsetf(std::ios::dec); \ + if ((stream.peek() == '-') && std::is_unsigned::value) { \ + return false; \ + } \ if ((stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) { \ return true; \ } \ diff --git a/test/integration/load_node_test.cpp b/test/integration/load_node_test.cpp index 1d86f51..9a93a68 100644 --- a/test/integration/load_node_test.cpp +++ b/test/integration/load_node_test.cpp @@ -32,6 +32,11 @@ TEST(LoadNodeTest, NumericConversion) { EXPECT_EQ(-std::numeric_limits::infinity(), node[4].as()); EXPECT_EQ(21, node[5].as()); EXPECT_EQ(13, node[6].as()); + // Throw exception: convert a negative number to an unsigned number. + EXPECT_THROW(node[7].as(), TypedBadConversion); + EXPECT_THROW(node[7].as(), TypedBadConversion); + EXPECT_THROW(node[7].as(), TypedBadConversion); + EXPECT_THROW(node[7].as(), TypedBadConversion); } TEST(LoadNodeTest, Binary) {