Skip to content

Commit

Permalink
Merge pull request #3378 from myk002/myk_remote_server
Browse files Browse the repository at this point in the history
[RemoteServer] fix crash on malformed json
  • Loading branch information
myk002 committed May 17, 2023
2 parents 1350145 + f265767 commit 2e1aae3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/changelog.txt
Expand Up @@ -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

## Fixes
- RemoteServer: fix crash on malformed json in ``dfhack-config/remote-server.json``
- `autolabor`: work detail override warning now only appears on the work details screen

## Misc Improvements
Expand Down
6 changes: 5 additions & 1 deletion library/RemoteClient.cpp
Expand Up @@ -150,7 +150,11 @@ int RemoteClient::GetDefaultPort()
if (in_file)
{
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();
if (config.isMember("port")) {
port = config["port"].asInt();
Expand Down
19 changes: 11 additions & 8 deletions library/RemoteServer.cpp
Expand Up @@ -420,17 +420,20 @@ ServerMainImpl::ServerMainImpl(std::promise<bool> promise, int port) :

Json::Value configJson;

std::ifstream inFile(filename, std::ios_base::in);

bool allow_remote = false;

if (inFile.is_open())
{
inFile >> configJson;
inFile.close();

allow_remote = configJson.get("allow_remote", "false").asBool();
std::ifstream inFile(filename, std::ios_base::in);
try {
if (inFile.is_open())
{
inFile >> configJson;
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
configJson["allow_remote"] = allow_remote;
Expand Down

0 comments on commit 2e1aae3

Please sign in to comment.