From 106349782888dca11465f96f8071ead627495636 Mon Sep 17 00:00:00 2001 From: myk002 Date: Tue, 2 Feb 2021 10:50:21 -0800 Subject: [PATCH] don't crash on malformed orders json --- docs/changelog.txt | 3 +++ plugins/orders.cpp | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index 989a9c558..180021ea4 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -33,6 +33,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: # Future +## Fixes +- `orders`: don't crash when importing orders with malformed JSON + # 0.47.04-r5 ## Fixes diff --git a/plugins/orders.cpp b/plugins/orders.cpp index 6c6888c0e..ab683f949 100644 --- a/plugins/orders.cpp +++ b/plugins/orders.cpp @@ -426,29 +426,38 @@ static command_result orders_import_command(color_ostream & out, const std::stri return CR_WRONG_USAGE; } + const std::string filename("dfhack-config/orders/" + name + ".json"); Json::Value orders; { - std::ifstream file("dfhack-config/orders/" + name + ".json"); + std::ifstream file(filename); if (!file.good()) { - out << COLOR_LIGHTRED << "Cannot find orders file." << std::endl; + out << COLOR_LIGHTRED << "Cannot find orders file: " << filename << std::endl; return CR_FAILURE; } - file >> orders; + try + { + file >> orders; + } + catch (const std::exception & e) + { + out << COLOR_LIGHTRED << "Error reading orders file: " << filename << ": " << e.what() << std::endl; + return CR_FAILURE; + } if (!file.good()) { - out << COLOR_LIGHTRED << "Error reading orders file." << std::endl; + out << COLOR_LIGHTRED << "Error reading orders file: " << filename << std::endl; return CR_FAILURE; } } if (orders.type() != Json::arrayValue) { - out << COLOR_LIGHTRED << "Invalid orders file: expected array" << std::endl; + out << COLOR_LIGHTRED << "Invalid orders file: " << filename << ": expected array" << std::endl; return CR_FAILURE; }