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.
This commit is contained in:
shweet committed 2026-11-22 19:52:33 -05:00
1 parent 068d189e6a
commit e963bc50d3
2 files changed
+36

No files matched your search

+23
View File
@@ -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<int Settings::*, int> 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)
+13
View File
@@ -9,6 +9,8 @@
#include <fstream>
#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<std::string>{});
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);
}