Throw an exception when trying to parse a negative number as an unsigned.

Fixing issue 859.
This commit is contained in:
Chen
2020-05-20 00:48:22 +08:00
committed by GitHub
parent 4b98aedc16
commit d9c35b6079
3 changed files with 10 additions and 1 deletions

View File

@@ -4,7 +4,7 @@
## HEAD ##
_none_
* Throws an exception when trying to parse a negative number as an unsigned integer.
## 0.6.0 ##

View File

@@ -15,6 +15,7 @@
#include <sstream>
#include <type_traits>
#include <vector>
#include <type_traits>
#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<type>::value) { \
return false; \
} \
if ((stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) { \
return true; \
} \

View File

@@ -32,6 +32,11 @@ TEST(LoadNodeTest, NumericConversion) {
EXPECT_EQ(-std::numeric_limits<float>::infinity(), node[4].as<float>());
EXPECT_EQ(21, node[5].as<int>());
EXPECT_EQ(13, node[6].as<int>());
// Throw exception: convert a negative number to an 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) {