From 9f87fd464a6133882fd2ab129fb6d3e90c61c8ac Mon Sep 17 00:00:00 2001
From: Niels <niels.lohmann@gmail.com>
Date: Sun, 15 Feb 2015 17:07:19 +0100
Subject: [PATCH] fixed Valgrind error

---
 src/json.hpp      | 6 ++++++
 src/json.hpp.re2c | 6 ++++++
 2 files changed, 12 insertions(+)

diff --git a/src/json.hpp b/src/json.hpp
index 1a09464b..08925890 100644
--- a/src/json.hpp
+++ b/src/json.hpp
@@ -3422,6 +3422,12 @@ basic_json_parser_59:
 
                             if (codepoint >= 0xD800 and codepoint <= 0xDBFF)
                             {
+                                // make sure there is a subsequent unicode
+                                if (m_cursor - i < 11 and * (i + 5) == '\\' and * (i + 6) == 'u')
+                                {
+                                    throw std::invalid_argument("missing low surrogate");
+                                }
+
                                 // get code yyyy from uxxxx\uyyyy
                                 auto codepoint2 = std::strtoul(std::string(i + 7, 4).c_str(), nullptr, 16);
                                 result += to_unicode(codepoint, codepoint2);
diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c
index f02111da..1aa5b94e 100644
--- a/src/json.hpp.re2c
+++ b/src/json.hpp.re2c
@@ -2771,6 +2771,12 @@ class basic_json
 
                             if (codepoint >= 0xD800 and codepoint <= 0xDBFF)
                             {
+                                // make sure there is a subsequent unicode
+                                if (m_cursor - i < 11 and * (i + 5) == '\\' and * (i + 6) == 'u')
+                                {
+                                    throw std::invalid_argument("missing low surrogate");
+                                }
+
                                 // get code yyyy from uxxxx\uyyyy
                                 auto codepoint2 = std::strtoul(std::string(i + 7, 4).c_str(), nullptr, 16);
                                 result += to_unicode(codepoint, codepoint2);