Skip to content

Commit 8fcbdad

Browse files
committed
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.
1 parent f7bdcfc commit 8fcbdad

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/test/settings_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ BOOST_AUTO_TEST_CASE(ReadWrite)
8080
BOOST_CHECK(values.empty());
8181
BOOST_CHECK(errors.empty());
8282

83-
// Check duplicate keys not allowed
83+
// Check duplicate keys not allowed and that values returns empty if a duplicate is found.
8484
WriteText(path, R"({
8585
"dupe": "string",
8686
"dupe": "dupe"
8787
})");
8888
BOOST_CHECK(!util::ReadSettings(path, values, errors));
8989
std::vector<std::string> dup_keys = {strprintf("Found duplicate key dupe in settings file %s", fs::PathToString(path))};
9090
BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end());
91+
BOOST_CHECK(values.empty());
9192

9293
// Check non-kv json files not allowed
9394
WriteText(path, R"("non-kv")");

src/util/settings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ bool ReadSettings(const fs::path& path, std::map<std::string, SettingsValue>& va
9999
auto inserted = values.emplace(in_keys[i], in_values[i]);
100100
if (!inserted.second) {
101101
errors.emplace_back(strprintf("Found duplicate key %s in settings file %s", in_keys[i], fs::PathToString(path)));
102+
values.clear();
103+
break;
102104
}
103105
}
104106
return errors.empty();

0 commit comments

Comments
 (0)