Skip to content

Commit fac932b

Browse files
author
MarcoFalke
committed
refactor: Use util::Split to avoid a harmless unsigned-integer-overflow
The previous commit added a test which would fail the unsigned-integer-overflow sanitizer. The warning is harmless and can be triggered on any commit, since the code was introduced. For reference, the warning would happen when the separator `-` was not present. For example: GET /rest/getutxos/6a297bfa5cb8dd976ab0207a767d6cbfaa5e876f30081127ec8674c8c52b16c0_+1.json would result in: rest.cpp:792:77: runtime error: unsigned integer overflow: 18446744073709551615 + 1 cannot be represented in type 'size_type' (aka 'unsigned long') #0 0x55ad42c16931 in rest_getutxos(std::any const&, HTTPRequest*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) src/rest.cpp:792:77 josibake#1 0x55ad4319e3c0 in std::function<bool (HTTPRequest*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)>::operator()(HTTPRequest*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&) const /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/std_function.h:591:9 josibake#2 0x55ad4319e3c0 in HTTPWorkItem::operator()() src/httpserver.cpp:59:9 josibake#3 0x55ad431a3eea in WorkQueue<HTTPClosure>::Run() src/httpserver.cpp:114:13 josibake#4 0x55ad4318f961 in HTTPWorkQueueRun(WorkQueue<HTTPClosure>*, int) src/httpserver.cpp:403:12 josibake#5 0x7f078ebcbbb3 (/lib/x86_64-linux-gnu/libstdc++.so.6+0xeabb3) (BuildId: 40b9b0d17fdeebfb57331304da2b7f85e1396ef2) josibake#6 0x55ad4277e01c in asan_thread_start(void*) asan_interceptors.cpp.o josibake#7 0x7f078e840a93 (/lib/x86_64-linux-gnu/libc.so.6+0x9ca93) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3) bitcoin#8 0x7f078e8cdc3b (/lib/x86_64-linux-gnu/libc.so.6+0x129c3b) (BuildId: 08134323d00289185684a4cd177d202f39c2a5f3) SUMMARY: UndefinedBehaviorSanitizer: unsigned-integer-overflow rest.cpp:792:77
1 parent fab54db commit fac932b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/rest.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -788,15 +788,17 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
788788

789789
for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++)
790790
{
791-
std::string strTxid = uriParts[i].substr(0, uriParts[i].find('-'));
792-
std::string strOutput = uriParts[i].substr(uriParts[i].find('-')+1);
793-
auto output{ToIntegral<uint32_t>(strOutput)};
791+
const auto txid_out{util::Split<std::string_view>(uriParts[i], '-')};
792+
if (txid_out.size() != 2) {
793+
return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
794+
}
795+
auto output{ToIntegral<uint32_t>(txid_out.at(1))};
794796

795-
if (!output || !IsHex(strTxid)) {
797+
if (!output || !IsHex(txid_out.at(0))) {
796798
return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
797799
}
798800

799-
vOutPoints.emplace_back(TxidFromString(strTxid), *output);
801+
vOutPoints.emplace_back(TxidFromString(txid_out.at(0)), *output);
800802
}
801803

802804
if (vOutPoints.size() > 0)

0 commit comments

Comments
 (0)