From 5b1aae12ca4a99c6b09349981a4902717a6a5d3e Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Mon, 7 Mar 2022 13:29:46 -0500 Subject: [PATCH] qt: Avoid crash on startup if int specified in settings.json Fix GUI startup crash reported by Rspigler in https://github.com/bitcoin/bitcoin/issues/24457 that happens if settings.json contains an integer value for any of the configuration options which GUI settings can currently clash with (-dbcache, -par, -spendzeroconfchange, -signer, -upnp, -natpmp, -listen, -server, -proxy, -proxy, -onion, -onion, -lang, and -prune). Fix is a one-line change in ArgsManager::GetArg. --- src/qt/test/optiontests.cpp | 8 ++++---- src/test/getarg_tests.cpp | 6 +++--- src/util/system.cpp | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/qt/test/optiontests.cpp b/src/qt/test/optiontests.cpp index 80d8d77984f..51894e1915f 100644 --- a/src/qt/test/optiontests.cpp +++ b/src/qt/test/optiontests.cpp @@ -15,15 +15,15 @@ //! Entry point for BitcoinApplication tests. void OptionTests::optionTests() { - // Test regression https://github.com/bitcoin/bitcoin/issues/24457. Check - // if setting an integer prune value causes an exception to be thrown in - // the OptionsModel constructor. + // Test regression https://github.com/bitcoin/bitcoin/issues/24457. Ensure + // that setting integer prune value doesn't cause an exception to be thrown + // in the OptionsModel constructor gArgs.LockSettings([&](util::Settings& settings) { settings.forced_settings.erase("prune"); settings.rw_settings["prune"] = 3814; }); gArgs.WriteSettingsFile(); - QVERIFY_EXCEPTION_THROWN(OptionsModel{}, std::runtime_error); + OptionsModel{}; gArgs.LockSettings([&](util::Settings& settings) { settings.rw_settings.erase("prune"); }); diff --git a/src/test/getarg_tests.cpp b/src/test/getarg_tests.cpp index 4dc12ec7d74..c877105fe78 100644 --- a/src/test/getarg_tests.cpp +++ b/src/test/getarg_tests.cpp @@ -98,21 +98,21 @@ BOOST_AUTO_TEST_CASE(setting_args) set_foo(99); BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "99"); - BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error); + BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "99"); BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 99); BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error); BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error); set_foo(3.25); BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "3.25"); - BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error); + BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "3.25"); BOOST_CHECK_THROW(args.GetIntArg("foo", 100), std::runtime_error); BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error); BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error); set_foo(0); BOOST_CHECK_EQUAL(args.GetSetting("foo").write(), "0"); - BOOST_CHECK_THROW(args.GetArg("foo", "default"), std::runtime_error); + BOOST_CHECK_EQUAL(args.GetArg("foo", "default"), "0"); BOOST_CHECK_EQUAL(args.GetIntArg("foo", 100), 0); BOOST_CHECK_THROW(args.GetBoolArg("foo", true), std::runtime_error); BOOST_CHECK_THROW(args.GetBoolArg("foo", false), std::runtime_error); diff --git a/src/util/system.cpp b/src/util/system.cpp index 1ad701c56d9..8e45453d310 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -591,7 +591,7 @@ bool ArgsManager::IsArgNegated(const std::string& strArg) const std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const { const util::SettingsValue value = GetSetting(strArg); - return value.isNull() ? strDefault : value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str(); + return value.isNull() ? strDefault : value.isFalse() ? "0" : value.isTrue() ? "1" : value.isNum() ? value.getValStr() : value.get_str(); } int64_t ArgsManager::GetIntArg(const std::string& strArg, int64_t nDefault) const