Browse Source

Merge branch 'trac2992'

fujiwara 12 years ago
parent
commit
95cb383de7
2 changed files with 28 additions and 4 deletions
  1. 9 4
      src/lib/cc/data.cc
  2. 19 0
      src/lib/cc/tests/data_unittests.cc

+ 9 - 4
src/lib/cc/data.cc

@@ -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);
         }
     }

+ 19 - 0
src/lib/cc/tests/data_unittests.cc

@@ -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);
 
 }