Merge bitcoin/bitcoin#27236: util: fix argsman dupe key error

8fcbdadfad8a9b3143527141ff37e5fe1e87f3b3 util: fix argsman dupe key error (willcl-ark)

Pull request description:

  fixes #22638

  Make GUI "Settings file could not be read. Do you want to reset settings to default values?" dialog actually clear all settings instead of partially keeping them when `settings.json` contains duplicate keys. This change has no effect on `bitcoind` because it treats a corrupt `settings.json` file as a hard error and doesn't attempt to modify it.

  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.

  The test added only checks that `values` is empty after a duplicate key is detected. This paves the way for the `abort` option in the GUI to properly clear `settings.json`, if the user selects the option, but the test does not currently check this entire mechanism (e.g. the file contents).

ACKs for top commit:
  TheCharlatan:
    Code review ACK 8fcbdadfad8a9b3143527141ff37e5fe1e87f3b3
  ryanofsky:
    Code review ACK 8fcbdadfad8a9b3143527141ff37e5fe1e87f3b3. Thanks for the fix! I would maybe update the PR description or add to it to describe behavior change of this PR:

Tree-SHA512: a5fd49b30ede0a24188623192825bccb952e427cc35f96ff9bfdc737361dcc35ac6480589ddf7f0ddeaebd34361bdaee31e7a91f2c0d857e4ff682614bb6bc04
This commit is contained in:
fanquake 2023-03-11 11:03:42 +01:00 committed by pasta
parent 74c6e38530
commit 6f6b718f78
No known key found for this signature in database
GPG Key ID: E2F3D7916E722D38
2 changed files with 4 additions and 1 deletions

View File

@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(ReadWrite)
BOOST_CHECK(values.empty()); BOOST_CHECK(values.empty());
BOOST_CHECK(errors.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"({ WriteText(path, R"({
"dupe": "string", "dupe": "string",
"dupe": "dupe" "dupe": "dupe"
@ -88,6 +88,7 @@ BOOST_AUTO_TEST_CASE(ReadWrite)
BOOST_CHECK(!util::ReadSettings(path, values, errors)); BOOST_CHECK(!util::ReadSettings(path, values, errors));
std::vector<std::string> dup_keys = {strprintf("Found duplicate key dupe in settings file %s", fs::PathToString(path))}; std::vector<std::string> 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_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end());
BOOST_CHECK(values.empty());
// Check non-kv json files not allowed // Check non-kv json files not allowed
WriteText(path, R"("non-kv")"); WriteText(path, R"("non-kv")");

View File

@ -111,6 +111,8 @@ bool ReadSettings(const fs::path& path, std::map<std::string, SettingsValue>& va
auto inserted = values.emplace(in_keys[i], in_values[i]); auto inserted = values.emplace(in_keys[i], in_values[i]);
if (!inserted.second) { if (!inserted.second) {
errors.emplace_back(strprintf("Found duplicate key %s in settings file %s", in_keys[i], fs::PathToString(path))); errors.emplace_back(strprintf("Found duplicate key %s in settings file %s", in_keys[i], fs::PathToString(path)));
values.clear();
break;
} }
} }
return errors.empty(); return errors.empty();