about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Gamari <ben@smart-cactus.org>2017-12-15T00·09-0500
committerBen Gamari <ben@smart-cactus.org>2017-12-15T00·09-0500
commitf9bcbddef260872878708317e9f5fa78cd0fe849 (patch)
treed15bf37daeb104229f4ecc6934cf9dffdce390bb
parent5d5b931fb178046ba286b8ef2b56a00b3a85c51c (diff)
json-to-value: Throw sensible error message on invalid numbers
-rw-r--r--src/libexpr/json-to-value.cc14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/libexpr/json-to-value.cc b/src/libexpr/json-to-value.cc
index 9380de3a66b3..8b1404595548 100644
--- a/src/libexpr/json-to-value.cc
+++ b/src/libexpr/json-to-value.cc
@@ -106,10 +106,16 @@ static void parseJSON(EvalState & state, const char * & s, Value & v)
             tmp_number += *s++;
         }
 
-        if (number_type == tFloat)
-            mkFloat(v, stod(tmp_number));
-        else
-            mkInt(v, stoi(tmp_number));
+        try {
+            if (number_type == tFloat)
+                mkFloat(v, stod(tmp_number));
+            else
+                mkInt(v, stoi(tmp_number));
+        } catch (std::invalid_argument e) {
+            throw JSONParseError("invalid JSON number");
+        } catch (std::out_of_range e) {
+            throw JSONParseError("out-of-range JSON number");
+        }
     }
 
     else if (strncmp(s, "true", 4) == 0) {