fix crash on malformed json (again)

develop
Myk Taylor 2023-05-16 11:26:47 -07:00
parent 2a734b92f7
commit f05fe33307
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
3 changed files with 17 additions and 9 deletions

@ -44,6 +44,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
- `work-now`: reinstated, renamed from ``workNow``: reduce the time that dwarves are left without a task after completing a job - `work-now`: reinstated, renamed from ``workNow``: reduce the time that dwarves are left without a task after completing a job
## Fixes ## Fixes
- RemoteServer: fix crash on malformed json in ``dfhack-config/remote-server.json``
## Misc Improvements ## Misc Improvements
- Terminal console no longer appears in front of the game window on startup - Terminal console no longer appears in front of the game window on startup

@ -150,7 +150,11 @@ int RemoteClient::GetDefaultPort()
if (in_file) if (in_file)
{ {
Json::Value config; Json::Value config;
in_file >> config; try {
in_file >> config;
} catch (const std::exception & e) {
std::cerr << "Error reading remote server config file: " << filename << ": " << e.what() << std::endl;
}
in_file.close(); in_file.close();
if (config.isMember("port")) { if (config.isMember("port")) {
port = config["port"].asInt(); port = config["port"].asInt();

@ -420,17 +420,20 @@ ServerMainImpl::ServerMainImpl(std::promise<bool> promise, int port) :
Json::Value configJson; Json::Value configJson;
std::ifstream inFile(filename, std::ios_base::in);
bool allow_remote = false; bool allow_remote = false;
if (inFile.is_open()) std::ifstream inFile(filename, std::ios_base::in);
{ try {
inFile >> configJson; if (inFile.is_open())
inFile.close(); {
inFile >> configJson;
allow_remote = configJson.get("allow_remote", "false").asBool(); allow_remote = configJson.get("allow_remote", "false").asBool();
}
} catch (const std::exception & e) {
std::cerr << "Error reading remote server config file: " << filename << ": " << e.what() << std::endl;
std::cerr << "Reverting to remote server config to defaults" << std::endl;
} }
inFile.close();
// rewrite/normalize config file // rewrite/normalize config file
configJson["allow_remote"] = allow_remote; configJson["allow_remote"] = allow_remote;