From d91067220faa5e3761b48ee89fa7ced1a9257236 Mon Sep 17 00:00:00 2001
From: Niels <niels.lohmann@gmail.com>
Date: Thu, 13 Oct 2016 21:00:48 +0200
Subject: [PATCH] added missing overflow check for #329

---
 README.md                    | 2 +-
 src/json.hpp                 | 7 +++++++
 src/json.hpp.re2c            | 7 +++++++
 test/src/unit-regression.cpp | 6 ++++++
 4 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 0691419d..8fde28bc 100644
--- a/README.md
+++ b/README.md
@@ -516,7 +516,7 @@ To compile and run the tests, you need to execute
 $ make check
 
 ===============================================================================
-All tests passed (8905168 assertions in 35 test cases)
+All tests passed (8905169 assertions in 35 test cases)
 ```
 
 Alternatively, you can use [https://cmake.org](CMake) and run
diff --git a/src/json.hpp b/src/json.hpp
index 7e7701a9..35a211fc 100644
--- a/src/json.hpp
+++ b/src/json.hpp
@@ -8960,6 +8960,13 @@ basic_json_parser_63:
             {
                 // parse with strtod
                 result.m_value.number_float = str_to_float_t(static_cast<number_float_t*>(nullptr), NULL);
+
+                // replace infinity and NAN by null
+                if (not std::isfinite(result.m_value.number_float))
+                {
+                    type = value_t::null;
+                    result.m_value = basic_json::json_value();
+                }
             }
 
             // save the type
diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c
index c74bf11f..b9c538f9 100644
--- a/src/json.hpp.re2c
+++ b/src/json.hpp.re2c
@@ -8257,6 +8257,13 @@ class basic_json
             {
                 // parse with strtod
                 result.m_value.number_float = str_to_float_t(static_cast<number_float_t*>(nullptr), NULL);
+
+                // replace infinity and NAN by null
+                if (not std::isfinite(result.m_value.number_float))
+                {
+                    type = value_t::null;
+                    result.m_value = basic_json::json_value();
+                }
             }
 
             // save the type
diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp
index 6d93e744..fee861e9 100644
--- a/test/src/unit-regression.cpp
+++ b/test/src/unit-regression.cpp
@@ -489,4 +489,10 @@ TEST_CASE("regression tests")
         j["/this/that"_json_pointer] = 27;
         CHECK(j == json({{"this", {{"that", 27}}}}));
     }
+
+    SECTION("issue #329 - serialized value not always can be parsed")
+    {
+        json j = json::parse("22e2222");
+        CHECK(j == json());
+    }
 }