From cdeadac4f83bd004130c9a935fbbcdd643d5ac07 Mon Sep 17 00:00:00 2001 From: pasta Date: Sun, 4 Oct 2026 00:02:06 -0400 Subject: [PATCH] fix(qt): report malformed lang/font settings instead of aborting at startup OptionsModel::Init reads "lang" and the Dash font settings ("font-family", "font-scale", "font-weight-normal", "font-weight-bold") from settings.json before the loop that turns a univalue type error into the "Could not read setting" error. A value of unexpected type (an object or array) therefore threw out of Init, and dash-qt was terminated by an uncaught exception instead of showing the corrupt settings dialog. A bad "lang" value aborted even earlier: GetLangTerritory reads -lang via gArgs, which includes settings.json once InitConfig has run. The stored font weights were not read at all when -font-family was given on the command line, so a bad value passed Init and then threw from the gArgs.GetIntArg() calls in bitcoin.cpp. Report these reads with the same error as the loop, always read the stored font weights, and keep the previously selected language in GetLangTerritory when -lang can't be read so that OptionsModel::Init gets to report it. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/qt/bitcoin.cpp | 7 +++- src/qt/optionsmodel.cpp | 64 ++++++++++++++++++++++++++----------- src/qt/test/optiontests.cpp | 55 +++++++++++++++++++++++++++++++ src/qt/test/optiontests.h | 2 ++ 4 files changed, 109 insertions(+), 19 deletions(-) diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index 734a45a6c9d8..61c4e825ea43 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -119,7 +119,12 @@ static QString GetLangTerritory() if(!lang_territory_qsettings.isEmpty()) lang_territory = lang_territory_qsettings; // 3) -lang command line argument - lang_territory = QString::fromStdString(gArgs.GetArg("-lang", lang_territory.toStdString())); + try { + lang_territory = QString::fromStdString(gArgs.GetArg("-lang", lang_territory.toStdString())); + } catch (const std::exception&) { + // Keep the language selected above if "lang" in settings.json has an + // unexpected type. OptionsModel::Init reports it as an error. + } return lang_territory; } diff --git a/src/qt/optionsmodel.cpp b/src/qt/optionsmodel.cpp index e0f5adc3a690..12e077381cb7 100644 --- a/src/qt/optionsmodel.cpp +++ b/src/qt/optionsmodel.cpp @@ -204,8 +204,20 @@ void OptionsModel::addOverriddenOption(const std::string &option) // Writes all missing QSettings with their default values bool OptionsModel::Init(bilingual_str& error) { + // Handles exceptions thrown by univalue that can happen if settings in + // settings.json don't have the expected types. + const auto setting_error = [&](const std::string& setting, const std::exception& e) { + error.original = strprintf("Could not read setting \"%s\", %s.", setting, e.what()); + error.translated = tr("Could not read setting \"%1\", %2.").arg(QString::fromStdString(setting), e.what()).toStdString(); + return false; + }; + // Initialize display settings from stored settings. - language = QString::fromStdString(SettingToString(node().getPersistentSetting("lang"), "")); + try { + language = QString::fromStdString(SettingToString(node().getPersistentSetting("lang"), "")); + } catch (const std::exception& e) { + return setting_error("lang", e); + } checkAndMigrate(); @@ -256,9 +268,13 @@ bool OptionsModel::Init(bilingual_str& error) addOverriddenOption("-font-family"); } if (GUIUtil::fontsLoaded()) { - const QString font_name = QString::fromStdString( - SettingToString(node().getPersistentSetting("font-family"), "")); - GUIUtil::setActiveFont(font_name); + try { + const QString font_name = QString::fromStdString( + SettingToString(node().getPersistentSetting("font-family"), "")); + GUIUtil::setActiveFont(font_name); + } catch (const std::exception& e) { + return setting_error("font-family", e); + } } // Font Scale @@ -266,7 +282,11 @@ bool OptionsModel::Init(bilingual_str& error) addOverriddenOption("-font-scale"); } if (GUIUtil::fontsLoaded()) { - GUIUtil::setFontScale(SettingToInt(node().getPersistentSetting("font-scale"), GUIUtil::defaultFontScale())); + try { + GUIUtil::setFontScale(SettingToInt(node().getPersistentSetting("font-scale"), GUIUtil::defaultFontScale())); + } catch (const std::exception& e) { + return setting_error("font-scale", e); + } } // Font Weight (Normal) @@ -276,11 +296,17 @@ bool OptionsModel::Init(bilingual_str& error) const bool override_family{isOptionOverridden("-font-family")}; if (GUIUtil::fontsLoaded()) { - // If font was overridden by CLI but weight wasn't, use the font's default weight const int default_arg = GUIUtil::defaultWeightArg(GUIUtil::FontWeight::Normal); - const int arg = (!override_family || isOptionOverridden("-font-weight-normal")) - ? SettingToInt(node().getPersistentSetting("font-weight-normal"), default_arg) - : default_arg; + // Read the stored value even when it is not used below, so that a + // malformed one is reported here rather than thrown later via gArgs. + int setting_arg; + try { + setting_arg = SettingToInt(node().getPersistentSetting("font-weight-normal"), default_arg); + } catch (const std::exception& e) { + return setting_error("font-weight-normal", e); + } + // If font was overridden by CLI but weight wasn't, use the font's default weight + const int arg = (!override_family || isOptionOverridden("-font-weight-normal")) ? setting_arg : default_arg; if (!GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Normal, arg)) { node().forceSetting("font-weight-normal", default_arg); GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Normal, default_arg); @@ -292,11 +318,17 @@ bool OptionsModel::Init(bilingual_str& error) addOverriddenOption("-font-weight-bold"); } if (GUIUtil::fontsLoaded()) { - // If font was overridden by CLI but weight wasn't, use the font's default weight const int default_arg = GUIUtil::defaultWeightArg(GUIUtil::FontWeight::Bold); - const int arg = (!override_family || isOptionOverridden("-font-weight-bold")) - ? SettingToInt(node().getPersistentSetting("font-weight-bold"), default_arg) - : default_arg; + // Read the stored value even when it is not used below, so that a + // malformed one is reported here rather than thrown later via gArgs. + int setting_arg; + try { + setting_arg = SettingToInt(node().getPersistentSetting("font-weight-bold"), default_arg); + } catch (const std::exception& e) { + return setting_error("font-weight-bold", e); + } + // If font was overridden by CLI but weight wasn't, use the font's default weight + const int arg = (!override_family || isOptionOverridden("-font-weight-bold")) ? setting_arg : default_arg; if (!GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Bold, arg)) { node().forceSetting("font-weight-bold", default_arg); GUIUtil::setWeightFromArg(GUIUtil::FontWeight::Bold, default_arg); @@ -361,11 +393,7 @@ bool OptionsModel::Init(bilingual_str& error) try { getOption(option); } catch (const std::exception& e) { - // This handles exceptions thrown by univalue that can happen if - // settings in settings.json don't have the expected types. - error.original = strprintf("Could not read setting \"%s\", %s.", setting, e.what()); - error.translated = tr("Could not read setting \"%1\", %2.").arg(QString::fromStdString(setting), e.what()).toStdString(); - return false; + return setting_error(setting, e); } } diff --git a/src/qt/test/optiontests.cpp b/src/qt/test/optiontests.cpp index fba2d2f118f6..626b5d1079d1 100644 --- a/src/qt/test/optiontests.cpp +++ b/src/qt/test/optiontests.cpp @@ -101,6 +101,61 @@ void OptionTests::integerGetArgBug() gArgs.WriteSettingsFile(); } +void OptionTests::invalidFontSetting() +{ + if (QApplication::platformName() == "minimal") { + QSKIP("AppTests cannot initialize fonts with the 'minimal' platform plugin."); + } + + // Font settings are only read once loadFonts() has run, which AppTests does. + QVERIFY2(GUIUtil::fontsLoaded(), + "GUIUtil::loadFonts() must succeed in AppTests::appTests() before OptionTests run."); + + for (const std::string setting : {"font-family", "font-scale", "font-weight-normal", "font-weight-bold"}) { + gArgs.LockSettings([&](util::Settings& settings) { + settings.rw_settings[setting] = UniValue{UniValue::VARR}; + }); + bilingual_str error; + QVERIFY(!OptionsModel{m_node}.Init(error)); + QCOMPARE(QString::fromStdString(error.original), + QString::fromStdString(strprintf("Could not read setting \"%s\", JSON value of type array is not of expected type string.", setting))); + gArgs.LockSettings([&](util::Settings& settings) { + settings.rw_settings.erase(setting); + }); + } + + // With -font-family on the command line the stored weights are not used, but + // dash-qt still reads them through gArgs later, so they must be reported too. + for (const std::string setting : {"font-weight-normal", "font-weight-bold"}) { + gArgs.LockSettings([&](util::Settings& settings) { + settings.command_line_options["font-family"] = {UniValue{GUIUtil::defaultFontFamily().toStdString()}}; + settings.rw_settings[setting] = UniValue{UniValue::VARR}; + }); + bilingual_str error; + QVERIFY(!OptionsModel{m_node}.Init(error)); + QCOMPARE(QString::fromStdString(error.original), + QString::fromStdString(strprintf("Could not read setting \"%s\", JSON value of type array is not of expected type string.", setting))); + gArgs.LockSettings([&](util::Settings& settings) { + settings.command_line_options.erase("font-family"); + settings.rw_settings.erase(setting); + }); + } +} + +void OptionTests::invalidLangSetting() +{ + // A "lang" value with an unexpected type in settings.json must be reported + // through the error string like other settings instead of throwing out of + // OptionsModel::Init. + gArgs.LockSettings([&](util::Settings& settings) { + settings.rw_settings["lang"] = UniValue{UniValue::VOBJ}; + }); + bilingual_str error; + QVERIFY(!OptionsModel{m_node}.Init(error)); + QCOMPARE(QString::fromStdString(error.original), + QString{"Could not read setting \"lang\", JSON value of type object is not of expected type string."}); +} + void OptionTests::parametersInteraction() { // Test that the bug https://github.com/bitcoin-core/gui/issues/567 does not resurface. diff --git a/src/qt/test/optiontests.h b/src/qt/test/optiontests.h index 89976d97d0b3..2cee85ab5903 100644 --- a/src/qt/test/optiontests.h +++ b/src/qt/test/optiontests.h @@ -21,6 +21,8 @@ private Q_SLOTS: void init(); // called before each test function execution. void migrateSettings(); void integerGetArgBug(); + void invalidFontSetting(); + void invalidLangSetting(); void parametersInteraction(); void extractFilter(); void effectivePointSize();