-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Consolidate redundant implementations of ParseHashStr #14307
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
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.
Concept ACK. Looks like the new parse helper will reject strings that are hex but not exactly 64 chars. Could add a test that fails with this change, but passes without?
src/core_read.cpp
Outdated
@@ -193,14 +193,13 @@ bool DecodePSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, | |||
return true; | |||
} | |||
|
|||
uint256 ParseHashStr(const std::string& strHex, const std::string& strName) | |||
bool ParseHashStr(const std::string& strReq, uint256& v) |
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: Could keep the name strHex
and result
to minimize the diff?
src/core_read.cpp
Outdated
{ | ||
if (!IsHex(strHex)) // Note: IsHex("") is false | ||
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')"); | ||
if (!IsHex(strReq) || (strReq.size() != 64)) |
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: Redundant parentheses around strReq.size() != 64
:-)
uint256 txid = ParseHashStr(prevOut["txid"].get_str(), "txid"); | ||
uint256 txid; | ||
if (!ParseHashStr(prevOut["txid"].get_str(), txid)) { | ||
throw std::runtime_error("txid must be hexadecimal string (not '" + prevOut["txid"].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 know this is unchanged from the previous version, but could there be circumstances where this opens up for log file injection? Safer not to echo the input?
c76c7ad
to
dc95535
Compare
src/rpc/mining.cpp
Outdated
uint256 hash = ParseHashStr(request.params[0].get_str(), "txid"); | ||
uint256 hash; | ||
if (!ParseHashStr(request.params[0].get_str(), hash)) { | ||
throw JSONRPCError(RPC_INVALID_PARAMETER, "txid must be hexadecimal string (not '" + request.params[0].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 know this is unchanged from the previous version, but could there be circumstances where this opens up for log file injection? Safer not to echo the input?
Concept ACK Thanks for taking on this issue. Closing #14288. |
e063b15
to
a85bbe9
Compare
Reviewers, 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. |
a85bbe9
to
c5f066e
Compare
Rebased for #13424 |
c5f066e
to
0b5791e
Compare
Converted one parse from |
src/core_read.cpp
Outdated
{ | ||
if (!IsHex(strHex)) // Note: IsHex("") is false | ||
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')"); | ||
if (!IsHex(strHex) || (strHex.size() != 64)) |
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: Redundant parentheses around strHex.size() != 64
:-)
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.
Could go either way but I like explicit precedence to guide the eye.
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.
Perhaps swap the two boolean conditions? If the condition strHex.size()
is false then the function can return without having to evaluate the condition IsHex(strHex)
if ((strHex.size() != 64) || !IsHex(strHex)) return false;
src/bitcoin-tx.cpp
Outdated
uint256 txid(uint256S(strTxid)); | ||
uint256 txid; | ||
if (!ParseHashStr(vStrInputParts[0], txid)) { | ||
throw std::runtime_error("txid must be hexadecimal string (not '" + vStrInputParts[0] + "')"); |
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 know this is unchanged from the previous version, but could there be circumstances where this opens up for log file injection? Safer not to echo the input?
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.
This had been changed so I switched it back.
What about adding |
c60cfb7
to
f78c166
Compare
I like |
f78c166
to
909a8f6
Compare
@MarcoFalke added tests, only two of them fail before this change because of existing checks:
|
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.
utACK 909a8f6
Nice improvement!
src/core_io.h
Outdated
@@ -25,7 +25,7 @@ std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDeco | |||
bool DecodeHexTx(CMutableTransaction& tx, const std::string& hex_tx, bool try_no_witness = false, bool try_witness = true); | |||
bool DecodeHexBlk(CBlock&, const std::string& strHexBlk); | |||
bool DecodeHexBlockHeader(CBlockHeader&, const std::string& hex_header); | |||
uint256 ParseHashStr(const std::string&, const std::string& strName); | |||
bool ParseHashStr(const std::string& strHex, uint256& result); |
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.
Perhaps we can document bool ParseHashStr(const std::string& strHex, uint256& result)
and specify the pre-condition that the strHex
argument must be a hexadecimal string with a length of 64 bytes.
src/core_read.cpp
Outdated
{ | ||
if (!IsHex(strHex)) // Note: IsHex("") is false | ||
throw std::runtime_error(strName + " must be hexadecimal string (not '" + strHex + "')"); | ||
if (!IsHex(strHex) || (strHex.size() != 64)) |
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.
Perhaps swap the two boolean conditions? If the condition strHex.size()
is false then the function can return without having to evaluate the condition IsHex(strHex)
if ((strHex.size() != 64) || !IsHex(strHex)) return false;
This change: * adds a length check to ParseHashStr, appropriate given its use to populate a 256-bit number from a hex str. * allows the caller to handle the failure, which allows for the more appropriate JSONRPCError on failure in prioritisetransaction rpc
909a8f6
to
9c5af58
Compare
Switched the condition and added docs. |
utACK 9c5af58 |
* @param[out] result the result of the parasing | ||
* @returns true if successful, false if not | ||
* | ||
* @see ParseHashV for an RPC-oriented version of this |
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.
ParseHashV
could use ParseHashStr
?
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.
Happy to do that as a follow-up.
utACK 9c5af58. |
utACK 9c5af58 |
9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley) Pull request description: This change: * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate a 256-bit number from a hex str * allows the caller to handle the failure, which allows for the more appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc Relative to #14288 Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
Summary: 9c5af58d51 Consolidate redundant implementations of ParseHashStr (Ben Woosley) Pull request description: This change: * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate a 256-bit number from a hex str * allows the caller to handle the failure, which allows for the more appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc Relative to #14288 Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53 Backport of Core [[bitcoin/bitcoin#14307 | PR14307]] bitcoin/bitcoin#14307 Depends on D5294 Test Plan: ninja check ninja check-functional Reviewers: O1 Bitcoin ABC, #bitcoin_abc, Fabien Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D5295
Summary: 9c5af58d51 Consolidate redundant implementations of ParseHashStr (Ben Woosley) Pull request description: This change: * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate a 256-bit number from a hex str * allows the caller to handle the failure, which allows for the more appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc Relative to #14288 Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53 Backport of Core [[bitcoin/bitcoin#14307 | PR14307]] bitcoin/bitcoin#14307 Depends on D5294 Test Plan: ninja check ninja check-functional Reviewers: O1 Bitcoin ABC, #bitcoin_abc, Fabien Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D5295
…ashStr 9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley) Pull request description: This change: * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate a 256-bit number from a hex str * allows the caller to handle the failure, which allows for the more appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc Relative to bitcoin#14288 Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
…ashStr 9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley) Pull request description: This change: * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate a 256-bit number from a hex str * allows the caller to handle the failure, which allows for the more appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc Relative to bitcoin#14288 Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
…shStr 9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley) Pull request description: This change: * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate a 256-bit number from a hex str * allows the caller to handle the failure, which allows for the more appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc Relative to bitcoin#14288 Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
…shStr 9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley) Pull request description: This change: * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate a 256-bit number from a hex str * allows the caller to handle the failure, which allows for the more appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc Relative to bitcoin#14288 Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
…shStr 9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley) Pull request description: This change: * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate a 256-bit number from a hex str * allows the caller to handle the failure, which allows for the more appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc Relative to bitcoin#14288 Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
…shStr 9c5af58 Consolidate redundant implementations of ParseHashStr (Ben Woosley) Pull request description: This change: * adds a length check to all calls to `ParseHashStr`, appropriate given its use to populate a 256-bit number from a hex str * allows the caller to handle the failure, which allows for the more appropriate `JSONRPCError` on failure in `prioritisetransaction` rpc Relative to bitcoin#14288 Tree-SHA512: baa791147e5ceb3c30c70df3981aaf807bf7d4a90a0be3625540b59aa4b9a9d303a452bfef18bf167cbb833ef9591b4ef5948bf4a1ce67b421d804ae8d20ea53
This change:
ParseHashStr
, appropriate given its use to populatea 256-bit number from a hex str
appropriate
JSONRPCError
on failure inprioritisetransaction
rpcRelative to #14288