Merge pull request #1736 from bgamari/stoi-exceptions

Gracefully handle exceptions from stoi
This commit is contained in:
Eelco Dolstra 2017-12-15 11:39:43 +01:00 committed by GitHub
commit 1dffbff57d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View file

@ -106,10 +106,16 @@ static void parseJSON(EvalState & state, const char * & s, Value & v)
tmp_number += *s++; tmp_number += *s++;
} }
try {
if (number_type == tFloat) if (number_type == tFloat)
mkFloat(v, stod(tmp_number)); mkFloat(v, stod(tmp_number));
else else
mkInt(v, stoi(tmp_number)); 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) { else if (strncmp(s, "true", 4) == 0) {

View file

@ -52,7 +52,12 @@ struct CmdEdit : InstallableCommand
throw Error("cannot parse meta.position attribute '%s'", pos); throw Error("cannot parse meta.position attribute '%s'", pos);
std::string filename(pos, 0, colon); 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"); auto editor = getEnv("EDITOR", "cat");