about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2017-12-15T10·39+0100
committerGitHub <noreply@github.com>2017-12-15T10·39+0100
commit1dffbff57d427da098bb750b5a86e57d7d0a401b (patch)
treec8a1b94b50b0a5c4da40c80e9b34cd22fa797703
parent5d5b931fb178046ba286b8ef2b56a00b3a85c51c (diff)
parent626a94d70ea7a5c5b87f4d6061633c019ca8ea13 (diff)
Merge pull request #1736 from bgamari/stoi-exceptions
Gracefully handle exceptions from stoi
-rw-r--r--src/libexpr/json-to-value.cc14
-rw-r--r--src/nix/edit.cc7
2 files changed, 16 insertions, 5 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) {
diff --git a/src/nix/edit.cc b/src/nix/edit.cc
index 127be321eee2..7eaa86e2f914 100644
--- a/src/nix/edit.cc
+++ b/src/nix/edit.cc
@@ -52,7 +52,12 @@ struct CmdEdit : InstallableCommand
             throw Error("cannot parse meta.position attribute '%s'", pos);
 
         std::string filename(pos, 0, colon);
-        int lineno = std::stoi(std::string(pos, colon + 1));
+        int lineno;
+        try {
+            lineno = std::stoi(std::string(pos, colon + 1));
+        } catch (std::invalid_argument e) {
+            throw Error("cannot parse line number '%s'", pos);
+        }
 
         auto editor = getEnv("EDITOR", "cat");