From e963bc50d39f879c1a0b9a897634b7aa94bf6b90 Mon Sep 17 00:00:00 2001 From: shweet Date: Sun, 22 Nov 2026 19:52:33 -0500 Subject: [PATCH] Load an out-of-range enum setting as its default A hand-edited tool=99 or renderType=99 indexed past the end of an array. --- src/settings.cpp | 23 +++++++++++++++++++++++ tests/render/render.cpp | 13 +++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/settings.cpp b/src/settings.cpp index f7a05d3..3ae2f53 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -8,6 +8,23 @@ #include "log.h" #include "math_util.h" #include "path.h" +#include "tool.h" + +// Settings holding an enum value, with how many values it has. +constexpr std::pair SETTINGS_ENUM_COUNTS[] = { + {&Settings::theme, THEME_COUNT}, + {&Settings::language, LANGUAGE_COUNT}, + {&Settings::tool, TOOL_COUNT}, + {&Settings::renderType, RENDER_COUNT}, + {&Settings::renderFpsMode, FPS_COUNT}, + {&Settings::changeInterpolation, (int)Interpolation::COUNT}, + {&Settings::changeChildMode, (int)ChildMode::COUNT}, + {&Settings::timelineAddItemType, (int)ItemType::COUNT}, + {&Settings::timelineAddItemDestination, DESTINATION_THIS + 1}, + {&Settings::timelineAddItemSource, SOURCE_EXISTING + 1}, + {&Settings::previewOverlayDrawOrder, OVERLAY_UNDER + 1}, + {&Settings::mergeType, MERGE_IGNORE + 1}, + {&Settings::onionskinMode, (int)OnionskinMode::INDEX + 1}}; constexpr std::string_view SETTINGS_SHEAR = "Shear"; @@ -276,6 +293,12 @@ Settings::Settings(const std::filesystem::path& path) for (auto [scale, min, max] : {std::tuple{&uiScale, UI_SCALE_MIN, UI_SCALE_MAX}, std::tuple{&renderScale, RENDER_SCALE_MIN, RENDER_SCALE_MAX}}) *scale = std::clamp(*scale < min ? unit_to_percent(*scale) : *scale, min, max); + + // An enum value out of range (from a hand-edited file) goes back to its default. + Settings defaults = {}; + for (auto [member, count] : SETTINGS_ENUM_COUNTS) + if (this->*member < 0 || this->*member >= count) + this->*member = defaults.*member; } void Settings::save(const std::filesystem::path& path, const std::string& imguiData) diff --git a/tests/render/render.cpp b/tests/render/render.cpp index 103d45b..ceba941 100644 --- a/tests/render/render.cpp +++ b/tests/render/render.cpp @@ -9,6 +9,8 @@ #include #include "audio.h" +#include "file.h" +#include "settings.h" #include "canvas.h" #include "embed.h" #include "document.h" @@ -406,3 +408,14 @@ TEST_CASE("a second instance hands its paths to the first") CHECK(instance_paths_take() == std::vector{}); instance_stop(); } + +TEST_CASE("hand-edited settings out of an enum's range load as their defaults") +{ + auto path = std::filesystem::temp_directory_path() / "anm2ed_settings_test.ini"; + REQUIRE(file_text_save(path, "[Settings]\ntool=99\nrenderType=-3\ntheme=1\n")); + Settings settings(path); + CHECK(settings.tool == Settings{}.tool); + CHECK(settings.renderType == Settings{}.renderType); + CHECK(settings.theme == 1); + std::filesystem::remove(path); +}