fix issue 859

This commit is contained in:
dota17 2020-05-16 15:16:31 +08:00
parent 4b98aedc16
commit 776743bb6c
2 changed files with 13 additions and 0 deletions

View File

@ -15,6 +15,7 @@
#include <sstream> #include <sstream>
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#include <typeinfo>
#include "yaml-cpp/binary.h" #include "yaml-cpp/binary.h"
#include "yaml-cpp/node/impl.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(); \ const std::string& input = node.Scalar(); \
std::stringstream stream(input); \ std::stringstream stream(input); \
stream.unsetf(std::ios::dec); \ 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()) { \ if ((stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) { \
return true; \ return true; \
} \ } \

View File

@ -32,6 +32,11 @@ TEST(LoadNodeTest, NumericConversion) {
EXPECT_EQ(-std::numeric_limits<float>::infinity(), node[4].as<float>()); EXPECT_EQ(-std::numeric_limits<float>::infinity(), node[4].as<float>());
EXPECT_EQ(21, node[5].as<int>()); EXPECT_EQ(21, node[5].as<int>());
EXPECT_EQ(13, node[6].as<int>()); EXPECT_EQ(13, node[6].as<int>());
// Throw error: convert signed number to unsigned number.
EXPECT_THROW(node[7].as<unsigned>(), TypedBadConversion<unsigned int>);
EXPECT_THROW(node[7].as<unsigned short>(), TypedBadConversion<unsigned short>);
EXPECT_THROW(node[7].as<unsigned long>(), TypedBadConversion<unsigned long>);
EXPECT_THROW(node[7].as<unsigned long long>(), TypedBadConversion<unsigned long long>);
} }
TEST(LoadNodeTest, Binary) { TEST(LoadNodeTest, Binary) {