-
Notifications
You must be signed in to change notification settings - Fork 37.7k
qt: Avoid crash on startup if int specified in settings.json #24498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Just add tests. No changes to application behavior. Tests will be updated in the next commit changing & improving current behavior. Include a Qt test for GUI startup crash reported by Rspigler in bitcoin#24457 caused by GetArg behavior 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 GUI startup crash reported by Rspigler in bitcoin#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.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
Updated 81adab4 -> 5604310 ( |
Updated 5604310 -> e584e1e ( |
Updated e584e1e -> cfbcdd3 ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested ACK 84b0973.
Test regression fails without the fix:
FAIL! : OptionTests::optionTests() Caught unhandled exception
Loc: [qtestcase.cpp(1939)]
Totals: 1 passed, 1 failed, 0 skipped, 0 blacklisted, 1ms
********* Finished testing of OptionTests *********
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: JSON value is not a string as expected
#ifndef BITCOIN_QT_TEST_OPTIONTESTS_H | ||
#define BITCOIN_QT_TEST_OPTIONTESTS_H | ||
|
||
#include <qt/optionsmodel.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, move to .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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we've exceeded the threshold where the ternary operator is a helpful shorthand in this case. I'd really prefer to write this out in code (doesn't need to be in this PR, to be clear, but this is some terrible code to review imo).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had the same feeling, this is what I did locally:
if (value.isNull()) return strDefault;
if (value.isBool()) return value.isFalse() ? "0" : "1";
if (value.isNum()) return value.getValStr();
return value.get_str();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some cases the ternary operator is used in place for switch-case. If this is one such case, I think it is acceptable. Though, it might be better to put each case
onto a new line to clarify that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some cases the ternary operator is used in place for switch-case. If this is one such case, I think it is acceptable. Though, it might be better to put each
case
onto a new line to clarify that.
Yes. I was about to update this but it got merged. I don't think it is too bad, though. The line is just meant to be read as "If isNull, return default, if isFalse, return 0, if isTrue, return 1, if isNum, return getValStr, otherwise return get_str."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference my clang-format seems to understand this as well and produce:
diff --git a/src/util/system.cpp b/src/util/system.cpp
index 8e45453d31..8f37b030c1 100644
--- a/src/util/system.cpp
+++ b/src/util/system.cpp
@@ -591,7 +591,11 @@ 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.isNum() ? value.getValStr() : 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
Code review ACK 5b1aae1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the tests!
Code review ACK 5b1aae1
void OptionTests::optionTests() | ||
{ | ||
// Test regression https://github.com/bitcoin/bitcoin/issues/24457. Ensure | ||
// that setting integer prune value doesn't cause an exception to be thrown |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 5b1aae1 |
Just add tests. No changes to application behavior. Tests will be updated in the next commit changing & improving current behavior. Include a Qt test for GUI startup crash reported by Rspigler in bitcoin#24457 caused by GetArg behavior 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). Github-Pull: bitcoin#24498 Rebased-From: 84b0973
Fix GUI startup crash reported by Rspigler in bitcoin#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. Github-Pull: bitcoin#24498 Rebased-From: 5b1aae1
Backported in #24511. |
…ettings.json 5b1aae1 qt: Avoid crash on startup if int specified in settings.json (Ryan Ofsky) 84b0973 test: Add tests for GetArg methods / settings.json type coercion (Ryan Ofsky) Pull request description: Should probably add this change to 23.x as suggested by Luke bitcoin#24457 (comment). If settings like `prune` are added to `settings.json` in the future, it would be preferable for 23.x releases to respect the setting instead of crash. --- Fix GUI startup crash reported by Rspigler in bitcoin#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). The fix is a one-line change in `ArgsManager::GetArg`. The rest of the PR just adds a regression test for the GUI and unit tests for ArgsManager::GetArg methods. ACKs for top commit: laanwj: Code review ACK 5b1aae1 achow101: ACK 5b1aae1 jonatack: Code review ACK 5b1aae1 Tree-SHA512: 958991b4bead9b82a3879fdca0f8d6405e2a212b7c46cf356f078843a4f156e27fd75fc46e2013aa5159582ead06d343c1ed248d678b3e5bbd312f247e37894c
…ettings.json 7e1b968 qt: Avoid crash on startup if int specified in settings.json (Ryan Ofsky) 4607f70 test: Add tests for GetArg methods / settings.json type coercion (Ryan Ofsky) Pull request description: Backport of #24498 ACKs for top commit: jonatack: ACK 7e1b968 Tree-SHA512: efe6ec4361858e50fd524db64141ad3622ecef321b99567da9650575558a9a9bdec0fc43113967cae2f23a1375132eed2d6ebf64d4aa91ac1c5f2f591a26ba74
Fix GUI startup crash reported by Rspigler in bitcoin#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. Github-Pull: bitcoin#24498 Rebased-From: 5b1aae1
…ettings.json 344537c qt: Avoid crash on startup if int specified in settings.json (Ryan Ofsky) Pull request description: Backport of #24498 to 22.x branch This was already backported to the 23.x branch in #24511, but vasild discovered the issue can affect 22.x as well #15936 (comment): > While testing this, 22.x crashes if `settings.json` contains `"prune": 1234` due to #24498 which was fixed in 23.0. So, if this PR is included in 24.x and a user upgrades to 24.x and then downgrades to 22.x his 22.x would crash at startup. It's not very important to backport this to 22.x because I can work around the issue other ways, but I do see 22.x is listed as an active branch https://github.com/bitcoin/bitcoin/branches, so if there is another 22.x release, it would be nice to have this fix and not have a wonky uncaught univalue exception waiting to be hit ACKs for top commit: laanwj: ACK 344537c Tree-SHA512: 479f7fb4b4b73ec85f28e97af9b21d54245b8ab4d246e50134b37f1726fe3c57cf388fe4973f8ccf1b9efa7663166d1fbeb631758b39d4490b5eab82d0c83d77
Fix GUI startup crash reported by Rspigler in bitcoin/bitcoin#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. Github-Pull: bitcoin/bitcoin#24498 Rebased-From: 5b1aae12ca4a99c6b09349981a4902717a6a5d3e
Fix GUI startup crash reported by Rspigler in bitcoin/bitcoin#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. Github-Pull: bitcoin/bitcoin#24498 Rebased-From: 5b1aae1 (cherry picked from commit 344537c)
Fix GUI startup crash reported by Rspigler in bitcoin/bitcoin#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. Github-Pull: bitcoin/bitcoin#24498 Rebased-From: 5b1aae1 (cherry picked from commit 344537c)
Just add tests. No changes to application behavior. Tests will be updated in the next commit changing & improving current behavior. Include a Qt test for GUI startup crash reported by Rspigler in bitcoin/bitcoin#24457 caused by GetArg behavior 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). Github-Pull: bitcoin/bitcoin#24498 Rebased-From: 84b0973e35dae63cd1b60199b481e24d54e58c97
Fix GUI startup crash reported by Rspigler in bitcoin/bitcoin#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. Github-Pull: bitcoin/bitcoin#24498 Rebased-From: 5b1aae12ca4a99c6b09349981a4902717a6a5d3e
Should probably add this change to 23.x as suggested by Luke #24457 (comment). If settings like
prune
are added tosettings.json
in the future, it would be preferable for 23.x releases to respect the setting instead of crash.Fix GUI startup crash reported by Rspigler in #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).The fix is a one-line change in
ArgsManager::GetArg
. The rest of the PR just adds a regression test for the GUI and unit tests for ArgsManager::GetArg methods.