Commit 95cb383d authored by fujiwara's avatar fujiwara
Browse files

Merge branch 'trac2992'

parents 638087de b2f0388d
......@@ -24,6 +24,8 @@
#include <iostream>
#include <string>
#include <sstream>
#include <cerrno>
#include <climits>
#include <boost/algorithm/string.hpp> // for iequals
......@@ -398,19 +400,22 @@ fromStringstreamNumber(std::istream& in, int& pos) {
std::string number = numberFromStringstream(in, pos);
errno = 0;
i = strtol(number.c_str(), &endptr, 10);
if (*endptr != '\0') {
d = strtod(number.c_str(), &endptr);
const char* ptr;
errno = 0;
d = strtod(ptr = number.c_str(), &endptr);
is_double = true;
if (*endptr != '\0') {
if (*endptr != '\0' || ptr == endptr) {
isc_throw(JSONError, std::string("Bad number: ") + number);
} else {
if (d == HUGE_VAL || d == -HUGE_VAL) {
if (errno != 0) {
isc_throw(JSONError, std::string("Number overflow: ") + number);
}
}
} else {
if (i == LONG_MAX || i == LONG_MIN) {
if ((i == LONG_MAX || i == LONG_MIN) && errno != 0) {
isc_throw(JSONError, std::string("Number overflow: ") + number);
}
}
......
......@@ -15,6 +15,7 @@
#include <gtest/gtest.h>
#include <boost/foreach.hpp>
#include <boost/assign/std/vector.hpp>
#include <climits>
#include <cc/data.h>
......@@ -146,6 +147,22 @@ TEST(Element, from_and_to_json) {
EXPECT_EQ("100", Element::fromJSON("1e2")->str());
EXPECT_EQ("100", Element::fromJSON("+1e2")->str());
EXPECT_EQ("-100", Element::fromJSON("-1e2")->str());
// LONG_MAX, -LONG_MAX, LONG_MIN test
std::ostringstream longmax, minus_longmax, longmin;
longmax << LONG_MAX;
minus_longmax << -LONG_MAX;
longmin << LONG_MIN;
EXPECT_NO_THROW( {
EXPECT_EQ(longmax.str(), Element::fromJSON(longmax.str())->str());
});
EXPECT_NO_THROW( {
EXPECT_EQ(minus_longmax.str(), Element::fromJSON(minus_longmax.str())->str());
});
EXPECT_NO_THROW( {
EXPECT_EQ(longmin.str(), Element::fromJSON(longmin.str())->str());
});
EXPECT_EQ("0.01", Element::fromJSON("1e-2")->str());
EXPECT_EQ("0.01", Element::fromJSON(".01")->str());
EXPECT_EQ("-0.01", Element::fromJSON("-1e-2")->str());
......@@ -173,6 +190,8 @@ TEST(Element, from_and_to_json) {
EXPECT_THROW(Element::fromJSON("-1.1e12345678901234567890")->str(), JSONError);
EXPECT_THROW(Element::fromJSON("1e12345678901234567890")->str(), JSONError);
EXPECT_THROW(Element::fromJSON("1e50000")->str(), JSONError);
// number underflow
EXPECT_THROW(Element::fromJSON("1.1e-12345678901234567890")->str(), JSONError);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment