Skip to content

Conversation

@practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Mar 5, 2020

Fail to parse whitespace-only strings in ParseMoney(...) (instead of parsing as 0).

This is a follow-up to #18225 ("util: Fail to parse empty string in ParseMoney") which made ParseMoney("") fail instead of parsing as 0.

Context: #18225 (comment)

Current non-test call sites:

$ git grep ParseMoney ":(exclude)src/test/"
src/bitcoin-tx.cpp:    if (!ParseMoney(strValue, value))
src/init.cpp:        if (!ParseMoney(gArgs.GetArg("-incrementalrelayfee", ""), n))
src/init.cpp:        if (!ParseMoney(gArgs.GetArg("-minrelaytxfee", ""), n)) {
src/init.cpp:        if (!ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n))
src/init.cpp:        if (!ParseMoney(gArgs.GetArg("-dustrelayfee", ""), n))
src/miner.cpp:    if (gArgs.IsArgSet("-blockmintxfee") && ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) {
src/util/moneystr.cpp:bool ParseMoney(const std::string& str, CAmount& nRet)
src/util/moneystr.h:NODISCARD bool ParseMoney(const std::string& str, CAmount& nRet);
src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-mintxfee", ""), n) || 0 == n) {
src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-fallbackfee", ""), nFeePerK)) {
src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-discardfee", ""), nFeePerK)) {
src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-paytxfee", ""), nFeePerK)) {
src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-maxtxfee", ""), nMaxFee)) {

Copy link
Member

@luke-jr luke-jr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK, though I prefer doing it another way

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

@maflcko
Copy link
Member

maflcko commented Mar 5, 2020

ACK 5a507f508b90e5e12800b9218dc54d52dcd38d05

@laanwj
Copy link
Member

laanwj commented Mar 6, 2020

utACK, though I prefer doing it another way

Same, this seems something that's better handled in the parsing code itself than as a pre-check calling out to an external function. Still, it's a good catch.

Do we need to accept trailing and leading whitespace at all here?

@practicalswift
Copy link
Contributor Author

practicalswift commented Mar 7, 2020

Do we need to accept trailing and leading whitespace at all here?

I don't have any strong opinions as long as it is done consistently (in order to avoid any gotchas for the callers of the function).

Perhaps we can move forward with this PR as-is to get the current behaviour consistent, and then take the discussion of removal of existing logic in a follow-up issue/PR?

@maflcko
Copy link
Member

maflcko commented Mar 12, 2020

ACK 43e4f1249ae2a9a1a150637c2d1d5aa8f544f46e

@practicalswift
Copy link
Contributor Author

@laanwj Updated. Does my implementation seem to be in line with your your suggestion? :)

@MarcoFalke Could you please re-review in light of the latest update? :)

@practicalswift practicalswift force-pushed the parsemoney-followup branch 2 times, most recently from e500e61 to 29f2e95 Compare March 12, 2020 14:11
@practicalswift practicalswift changed the title util: Fail to parse space-only strings in ParseMoney(...) (instead of parsing as zero) util: Fail to parse whitespace-only strings in ParseMoney(...) (instead of parsing as zero) Mar 12, 2020
@Empact
Copy link
Contributor

Empact commented Mar 12, 2020

ACK 100213c

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 100213c

@practicalswift
Copy link
Contributor Author

Ready for merge? :)

@sipa
Copy link
Member

sipa commented Mar 26, 2020

ACK 100213c

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TrimString drops \f\n\r\t\v, is this intended?

}
if (IsSpace(*p))
break;
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, if (IsSpace(*p)) return false;

for (; *p; p++)
if (!IsSpace(*p))
return false;
if (*p) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to L58, replace break; with if (*p) return false;.

@maflcko maflcko merged commit 0dc6218 into bitcoin:master Mar 27, 2020
@practicalswift
Copy link
Contributor Author

practicalswift commented Mar 27, 2020

TrimString drops \f\n\r\t\v, is this intended?

That is consistent with how the previously used IsSpace works, no?

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Mar 28, 2020
…arseMoney(...) (instead of parsing as zero)

100213c util: Fail to parse space-only strings in ParseMoney(...) (instead of parsing as zero) (practicalswift)

Pull request description:

  Fail to parse whitespace-only strings in `ParseMoney(...)` (instead of parsing as `0`).

  This is a follow-up to bitcoin#18225 ("util: Fail to parse empty string in `ParseMoney`") which made `ParseMoney("")` fail instead of parsing as `0`.

  Context: bitcoin#18225 (comment)

  Current non-test call sites:

  ```
  $ git grep ParseMoney ":(exclude)src/test/"
  src/bitcoin-tx.cpp:    if (!ParseMoney(strValue, value))
  src/init.cpp:        if (!ParseMoney(gArgs.GetArg("-incrementalrelayfee", ""), n))
  src/init.cpp:        if (!ParseMoney(gArgs.GetArg("-minrelaytxfee", ""), n)) {
  src/init.cpp:        if (!ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n))
  src/init.cpp:        if (!ParseMoney(gArgs.GetArg("-dustrelayfee", ""), n))
  src/miner.cpp:    if (gArgs.IsArgSet("-blockmintxfee") && ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) {
  src/util/moneystr.cpp:bool ParseMoney(const std::string& str, CAmount& nRet)
  src/util/moneystr.h:NODISCARD bool ParseMoney(const std::string& str, CAmount& nRet);
  src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-mintxfee", ""), n) || 0 == n) {
  src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-fallbackfee", ""), nFeePerK)) {
  src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-discardfee", ""), nFeePerK)) {
  src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-paytxfee", ""), nFeePerK)) {
  src/wallet/wallet.cpp:        if (!ParseMoney(gArgs.GetArg("-maxtxfee", ""), nMaxFee)) {
  ```

ACKs for top commit:
  Empact:
    ACK bitcoin@100213c
  sipa:
    ACK 100213c
  theStack:
    ACK bitcoin@100213c

Tree-SHA512: cadfb1ac8276cf54736c3444705f2650e7a08023673aedc729fabe751ae80f6c490fc0945ee38dbfd02c95e4d9853d1e4c84f5d3c310f44eaf3585afec8a4c22
MarkLTZ added a commit to litecoinz-core/litecoinz that referenced this pull request Apr 9, 2020
- rpc/wallet: initialize nFeeRequired to avoid using garbage value on failure bitcoin#18274
- httpserver: use own HTTP status codes bitcoin#18168
- tests: Add fuzzing harnesses for various Base{32,58,64} and hex related functions bitcoin#17229
- util: Don't allow Base32/64-decoding or ParseMoney(…) on strings with embedded NUL characters. Add tests. bitcoin#17753
- util: Fail to parse empty string in ParseMoney bitcoin#18225
- util: Fail to parse whitespace-only strings in ParseMoney(...) (instead of parsing as zero) bitcoin#18270
- Replace the LogPrint function with a macro bitcoin#17218
- Fix wallet unload race condition bitcoin#18338
- qt: Fix Window -> Minimize menu item bitcoin#18549
- windows: Enable heap terminate-on-corruption bitcoin#17916
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 12, 2021
… parsing as zero)

Summary: This is a backport of Core [[bitcoin/bitcoin#18270 | PR18270]]

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D8882
@practicalswift practicalswift deleted the parsemoney-followup branch April 10, 2021 19:40
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants