From f05fe333071a4d28cfdc643047376fa8a9bb24c6 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Tue, 16 May 2023 11:26:47 -0700 Subject: [PATCH] fix crash on malformed json (again) --- docs/changelog.txt | 1 + library/RemoteClient.cpp | 6 +++++- library/RemoteServer.cpp | 19 +++++++++++-------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index fc706cdba..50d015c4d 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -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`` ## Misc Improvements - Terminal console no longer appears in front of the game window on startup diff --git a/library/RemoteClient.cpp b/library/RemoteClient.cpp index 6a8becaae..0aa68eb51 100644 --- a/library/RemoteClient.cpp +++ b/library/RemoteClient.cpp @@ -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(); diff --git a/library/RemoteServer.cpp b/library/RemoteServer.cpp index 734b80702..77510d63a 100644 --- a/library/RemoteServer.cpp +++ b/library/RemoteServer.cpp @@ -420,17 +420,20 @@ ServerMainImpl::ServerMainImpl(std::promise 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;