From 4f98149fa784280b10fe4371e5a7d2f44f41f801 Mon Sep 17 00:00:00 2001 From: Robin Lambertz Date: Sat, 6 Mar 2021 12:40:29 +0100 Subject: [PATCH] api: Fix various crashes on bad settings data. (#186) getSetting now returns a straight nlohmann::json instead of an optional. If the data isn't present, it will return a json null. All accesses to the settings will first check that the data has the expected type. --- plugins/builtin/source/content/settings_entries.cpp | 2 +- plugins/libimhex/include/hex/api/content_registry.hpp | 2 +- plugins/libimhex/source/api/content_registry.cpp | 2 +- source/views/view_data_processor.cpp | 4 ++-- source/views/view_pattern.cpp | 4 ++-- source/window.cpp | 8 ++++---- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index 2dc60f8db..228c40e57 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -5,7 +5,7 @@ namespace hex::plugin::builtin { void registerSettings() { ContentRegistry::Settings::add("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", 0, [](auto name, nlohmann::json &setting) { - static int selection = setting; + static int selection = setting.is_number() ? static_cast(setting) : 0; const char* themes[] = { "hex.builtin.setting.interface.color.dark"_lang, diff --git a/plugins/libimhex/include/hex/api/content_registry.hpp b/plugins/libimhex/include/hex/api/content_registry.hpp index 585d32a5d..232683a22 100644 --- a/plugins/libimhex/include/hex/api/content_registry.hpp +++ b/plugins/libimhex/include/hex/api/content_registry.hpp @@ -51,7 +51,7 @@ namespace hex { static std::vector read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, const std::vector& defaultValue = { }); static std::map>& getEntries(); - static std::optional getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName); + static nlohmann::json getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName); static nlohmann::json& getSettingsData(); }; diff --git a/plugins/libimhex/source/api/content_registry.cpp b/plugins/libimhex/source/api/content_registry.cpp index 9eb2edad0..2182bd146 100644 --- a/plugins/libimhex/source/api/content_registry.cpp +++ b/plugins/libimhex/source/api/content_registry.cpp @@ -119,7 +119,7 @@ namespace hex { return SharedData::settingsEntries; } - std::optional ContentRegistry::Settings::getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName) { + nlohmann::json ContentRegistry::Settings::getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName) { auto &settings = getSettingsData(); if (!settings.contains(unlocalizedCategory)) return { }; diff --git a/source/views/view_data_processor.cpp b/source/views/view_data_processor.cpp index 7f515042b..c860190f0 100644 --- a/source/views/view_data_processor.cpp +++ b/source/views/view_data_processor.cpp @@ -20,8 +20,8 @@ namespace hex { View::subscribeEvent(Events::SettingsChanged, [](auto) { auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color"); - if (theme.has_value()) { - switch (static_cast(theme.value())) { + if (theme.is_number()) { + switch (static_cast(theme)) { default: case 0: /* Dark theme */ imnodes::StyleColorsDark(); diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index 31598dd44..d7e654cfa 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -182,8 +182,8 @@ namespace hex { View::subscribeEvent(Events::SettingsChanged, [this](auto) { auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color"); - if (theme.has_value()) { - switch (static_cast(theme.value())) { + if (theme.is_number()) { + switch (static_cast(theme)) { default: case 0: /* Dark theme */ this->m_textEditor.SetPalette(TextEditor::GetDarkPalette()); diff --git a/source/window.cpp b/source/window.cpp index af16656ce..6212eb177 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -61,8 +61,8 @@ namespace hex { { auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color"); - if (theme.has_value()) { - switch (static_cast(theme.value())) { + if (theme.is_number()) { + switch (static_cast(theme)) { default: case 0: /* Dark theme */ ImGui::StyleColorsDark(); @@ -87,8 +87,8 @@ namespace hex { { auto language = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.language"); - if (language.has_value()) - LangEntry::loadLanguage(static_cast(language.value())); + if (language.is_string()) + LangEntry::loadLanguage(static_cast(language)); } return { };