From f9bcbddef260872878708317e9f5fa78cd0fe849 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Thu, 14 Dec 2017 19:09:45 -0500 Subject: [PATCH 1/2] json-to-value: Throw sensible error message on invalid numbers --- src/libexpr/json-to-value.cc | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/libexpr/json-to-value.cc b/src/libexpr/json-to-value.cc index 9380de3a6..8b1404595 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) { From 626a94d70ea7a5c5b87f4d6061633c019ca8ea13 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Thu, 14 Dec 2017 19:11:56 -0500 Subject: [PATCH 2/2] edit: Catch stoi exceptions from line number parsing --- src/nix/edit.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/nix/edit.cc b/src/nix/edit.cc index 127be321e..7eaa86e2f 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");