Skip to content

Commit

Permalink
refactor: std::string -> std::string_view
Browse files Browse the repository at this point in the history
Safer after recent fix to make TxidFromString() respect string_view length. The internal call to uint256S() when parsing the txid would still have stopped at the '-'-symbol in this case though.
  • Loading branch information
hodlinator committed Jul 12, 2024
1 parent fed91bb commit 3d0aec1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,11 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
{
int32_t nOutput;
std::string strTxid = uriParts[i].substr(0, uriParts[i].find('-'));
std::string strOutput = uriParts[i].substr(uriParts[i].find('-')+1);
const std::vector<std::string_view> utxo_pair = util::Split<std::string_view>(uriParts[i], '-');
if (utxo_pair.size() != 2)
return RESTERR(req, HTTP_BAD_REQUEST, "Parse error: expected one '-' separator for UTXO in \"" + uriParts[i] + "\"");
const auto strTxid{utxo_pair[0]};
const auto strOutput{utxo_pair[1]};

if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid))
return RESTERR(req, HTTP_BAD_REQUEST, "Parse error: couldn't read \"<hexadecimal txid>-<decimal output index>\" from \"" + uriParts[i] + "\"");
Expand Down
4 changes: 2 additions & 2 deletions test/functional/interface_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ def run_test(self):
resp = self.test_rest_request("/getutxos/checkmempool", http_method='POST', req_type=ReqType.JSON, status=400, ret_type=RetType.OBJ)
assert_equal(resp.read().decode('utf-8').rstrip(), "Error: empty request")
resp = self.test_rest_request("/getutxos/checkmempool/", http_method='POST', req_type=ReqType.JSON, status=400, ret_type=RetType.OBJ)
assert_equal(resp.read().decode('utf-8').rstrip(), "Parse error: couldn't read \"<hexadecimal txid>-<decimal output index>\" from \"\"")
assert_equal(resp.read().decode('utf-8').rstrip(), "Parse error: expected one '-' separator for UTXO in \"\"")
resp = self.test_rest_request("/getutxos/checkmempool/-", http_method='POST', req_type=ReqType.JSON, status=400, ret_type=RetType.OBJ)
assert_equal(resp.read().decode('utf-8').rstrip(), "Parse error: couldn't read \"<hexadecimal txid>-<decimal output index>\" from \"-\"")
resp = self.test_rest_request("/getutxos/checkmempool/123", http_method='POST', req_type=ReqType.JSON, status=400, ret_type=RetType.OBJ)
assert_equal(resp.read().decode('utf-8').rstrip(), "Parse error: couldn't read \"<hexadecimal txid>-<decimal output index>\" from \"123\"")
assert_equal(resp.read().decode('utf-8').rstrip(), "Parse error: expected one '-' separator for UTXO in \"123\"")

# Test limits
long_uri = '/'.join([f"{txid}-{n_}" for n_ in range(20)])
Expand Down

0 comments on commit 3d0aec1

Please sign in to comment.