Merge bitcoin/bitcoin#36777: refactor: rpc: doc: Mark longpoll counter u32
What changed, and why it matters
This is a small code cleanup in Bitcoin Core's getblocktemplate RPC. It changes the type of an internal counter from 'unsigned int' to the explicitly-sized 'uint32_t' and documents that the long-poll counter is expected to be a 32-bit value. The patch does not change runtime behavior; it merely makes the existing implicit truncation explicit so that fuzzing and sanitizer tools don't flag it as suspicious. There is no direct security fix here, but making the code's assumptions explicit helps future testing catch real bugs.
No urgent action required. This is a refactor/documentation change. Operators and reviewers should note that the longpollid counter is now explicitly 32-bit; if a future change moves to LocaleIndependentAtoi<uint32_t>(), ensure it also handles malformed or out-of-range input gracefully.
Security signals we found
UBSan runtime error: implicit conversion from signed 64-bit long to unsigned int changes value
User-controlled input parsed via LocaleIndependentAtoi<int64_t> and assigned to unsigned int
Explicit cast added to make truncation visible, but no bounds check introduced
Change motivated by fuzz target (FUZZ=rpc) compatibility, not by a reported exploit
Evidence from the diff
In src/rpc/mining.cpp, nTransactionsUpdatedLast and nTransactionsUpdatedLastLP are changed from unsigned int to uint32_t. The longpollid parsing now explicitly casts the int64_t parsed from the user-supplied string to uint32_t. Previously, assigning a 64-bit signed value to an unsigned int caused an implicit conversion that UBSan reported as a value-changing truncation. The commit message states the counter is only ever a valid u32 in normal operation and that behavior is intentionally unchanged; the explicit cast and type change are to satisfy fuzzing (FUZZ=rpc) and UBSan.
Changed components
src/rpc/mining.cppgetblocktemplate RPC methodlongpollid parsingInspect captured patch +3 / −3
### src/rpc/mining.cpp
@@ -806,7 +806,7 @@ static RPCMethod getblocktemplate()
}
}
- static unsigned int nTransactionsUpdatedLast;
+ static uint32_t nTransactionsUpdatedLast;
const CTxMemPool& mempool = EnsureMemPool(node);
WAIT_LOCK(cs_main, cs_main_lock);
@@ -829,7 +829,7 @@ static RPCMethod getblocktemplate()
* The longpollid is assumed to be a tip hash if it has the right format.
*/
uint256 hashWatchedChain;
- unsigned int nTransactionsUpdatedLastLP;
+ uint32_t nTransactionsUpdatedLastLP;
if (lpval.isStr())
{
@@ -839,7 +839,7 @@ static RPCMethod getblocktemplate()
// Assume the longpollid is a block hash. If it's not then we return
// early below.
hashWatchedChain = ParseHashV(lpstr.substr(0, 64), "longpollid");
- nTransactionsUpdatedLastLP = LocaleIndependentAtoi<int64_t>(lpstr.substr(64));
+ nTransactionsUpdatedLastLP = static_cast<uint32_t>(LocaleIndependentAtoi<int64_t>(lpstr.substr(64)));
}
else
{Why this scored 21/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.