From 8fcbdadfad8a9b3143527141ff37e5fe1e87f3b3 Mon Sep 17 00:00:00 2001 From: willcl-ark Date: Thu, 9 Mar 2023 22:25:01 +0000 Subject: [PATCH] util: fix argsman dupe key error fixes #22638 If we find a duplicate key and error, clear `values` before returning so that WriteSettings will write an empty file, therefore clearing it. This aligns with GUI behaviour added in 1ee6d0b. --- src/test/settings_tests.cpp | 3 ++- src/util/settings.cpp | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/settings_tests.cpp b/src/test/settings_tests.cpp index 0feb68b9b1..ad12c46561 100644 --- a/src/test/settings_tests.cpp +++ b/src/test/settings_tests.cpp @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(ReadWrite) BOOST_CHECK(values.empty()); BOOST_CHECK(errors.empty()); - // Check duplicate keys not allowed + // Check duplicate keys not allowed and that values returns empty if a duplicate is found. WriteText(path, R"({ "dupe": "string", "dupe": "dupe" @@ -88,6 +88,7 @@ BOOST_AUTO_TEST_CASE(ReadWrite) BOOST_CHECK(!util::ReadSettings(path, values, errors)); std::vector dup_keys = {strprintf("Found duplicate key dupe in settings file %s", fs::PathToString(path))}; BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end()); + BOOST_CHECK(values.empty()); // Check non-kv json files not allowed WriteText(path, R"("non-kv")"); diff --git a/src/util/settings.cpp b/src/util/settings.cpp index 2b25b7f0e1..a2e30098dc 100644 --- a/src/util/settings.cpp +++ b/src/util/settings.cpp @@ -99,6 +99,8 @@ bool ReadSettings(const fs::path& path, std::map& va auto inserted = values.emplace(in_keys[i], in_values[i]); if (!inserted.second) { errors.emplace_back(strprintf("Found duplicate key %s in settings file %s", in_keys[i], fs::PathToString(path))); + values.clear(); + break; } } return errors.empty();