From 776743bb6c1b2abc6a91a8292cf67fd0515c4fbc Mon Sep 17 00:00:00 2001 From: dota17 Date: Sat, 16 May 2020 15:16:31 +0800 Subject: [PATCH] fix issue 859 --- include/yaml-cpp/node/convert.h | 8 ++++++++ test/integration/load_node_test.cpp | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/include/yaml-cpp/node/convert.h b/include/yaml-cpp/node/convert.h index d6bd365..7164022 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,13 @@ 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() == '-') && \ + (typeid(rhs) == typeid(unsigned) || \ + typeid(rhs) == typeid(unsigned short) || \ + typeid(rhs) == typeid(unsigned long) || \ + typeid(rhs) == typeid(unsigned long long))) { \ + 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..786e566 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 error: convert signed number to 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) {