diff --git a/src/common/settings.cpp b/src/common/settings.cpp index e9b929794ff..7d511b574de 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp @@ -86,7 +86,7 @@ bool ReadSettings(const fs::path& path, std::map& va SettingsValue in; if (!in.read(std::string{std::istreambuf_iterator(file), std::istreambuf_iterator()})) { - errors.emplace_back(strprintf("Settings file %s does not contain valid JSON. This is probably caused by disk corruption or a crash, " + errors.emplace_back(strprintf("Settings file %s does not contain valid JSON. This may be caused by a crash, power loss, full disk, or storage error, " "and can be fixed by removing the file, which will reset settings to default values.", fs::PathToString(path))); return false; @@ -139,7 +139,15 @@ bool WriteSettings(const fs::path& path, return false; } file << out.write(/* prettyIndent= */ 4, /* indentLevel= */ 1) << std::endl; + if (file.fail()) { + errors.emplace_back(strprintf("Error: Unable to write settings file %s", fs::PathToString(path))); + return false; + } file.close(); + if (file.fail()) { + errors.emplace_back(strprintf("Error: Unable to close settings file %s", fs::PathToString(path))); + return false; + } return true; } diff --git a/src/test/settings_tests.cpp b/src/test/settings_tests.cpp index 54d3b058118..0d8e3b1b7ae 100644 --- a/src/test/settings_tests.cpp +++ b/src/test/settings_tests.cpp @@ -101,7 +101,7 @@ BOOST_AUTO_TEST_CASE(ReadWrite) // Check invalid json not allowed WriteText(path, R"(invalid json)"); BOOST_CHECK(!common::ReadSettings(path, values, errors)); - std::vector fail_parse = {strprintf("Settings file %s does not contain valid JSON. This is probably caused by disk corruption or a crash, " + std::vector fail_parse = {strprintf("Settings file %s does not contain valid JSON. This may be caused by a crash, power loss, full disk, or storage error, " "and can be fixed by removing the file, which will reset settings to default values.", fs::PathToString(path))}; BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), fail_parse.begin(), fail_parse.end()); diff --git a/test/functional/feature_settings.py b/test/functional/feature_settings.py index 9a4138b9360..0585e056ff4 100755 --- a/test/functional/feature_settings.py +++ b/test/functional/feature_settings.py @@ -73,7 +73,7 @@ class SettingsTest(BitcoinTestFramework): # Test invalid json with settings.open("w") as fp: fp.write("invalid json") - node.assert_start_raises_init_error(expected_msg='does not contain valid JSON. This is probably caused by disk corruption or a crash', match=ErrorMatch.PARTIAL_REGEX) + node.assert_start_raises_init_error(expected_msg='does not contain valid JSON. This may be caused by a crash, power loss, full disk, or storage error', match=ErrorMatch.PARTIAL_REGEX) # Test invalid json object with settings.open("w") as fp: