miner: fix `addPackageTxs` unsigned integer overflow
What changed, and why it matters
This commit fixes a math bug in the Bitcoin Core block-building code. The original code subtracted a safety margin from the maximum block weight and then compared the current block weight to that lower number. Because the variables are unsigned integers, if the block weight was already larger than expected, the subtraction could underflow and wrap around to a huge number, making the comparison behave incorrectly. The fix rewrites the comparison as an addition on the other side, which cannot underflow. The practical effect is on when the miner decides to stop trying to add more transactions; the bug could make it give up too early or keep going too long in edge cases.
Treat as a low-severity correctness fix. Review whether any reachable configuration or runtime state could trigger the underflow, and consider backporting to maintained branches as a defensive measure. No immediate emergency response is warranted based on the diff alone.
Security signals we found
Unsigned integer underflow in block-weight comparison
Miner block-assembly logic correctness fix
Potential consensus-adjacent behavior in transaction selection
Defensive rewrite of arithmetic to avoid wraparound
Evidence from the diff
In src/node/miner.cpp, the condition nBlockWeight > m_options.nBlockMaxWeight - BLOCK_FULL_ENOUGH_WEIGHT_DELTA is changed to nBlockWeight + BLOCK_FULL_ENOUGH_WEIGHT_DELTA > m_options.nBlockMaxWeight. Both operands are unsigned (uint64_t/unsigned), so the original subtraction of BLOCK_FULL_ENOUGH_WEIGHT_DELTA from m_options.nBlockMaxWeight can underflow if nBlockMaxWeight is smaller than the delta, producing a very large value and causing the comparison to evaluate incorrectly. The rewritten form avoids the underflow because adding a positive delta to nBlockWeight cannot underflow. The change affects the early-exit heuristic in addPackageTxs when many consecutive transaction additions have failed.
Changed components
src/node/miner.cppBlockAssembler::addPackageTxsInspect captured patch +2 / −2
diff --git a/src/node/miner.cpp b/src/node/miner.cpp
index a08c70e2..e75bc3a6 100644
--- a/src/node/miner.cpp
+++ b/src/node/miner.cpp
@@ -397,8 +397,8 @@ void BlockAssembler::addPackageTxs(int& nPackagesSelected, int& nDescendantsUpda
++nConsecutiveFailed;
- if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
- m_options.nBlockMaxWeight - BLOCK_FULL_ENOUGH_WEIGHT_DELTA) {
+ if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight +
+ BLOCK_FULL_ENOUGH_WEIGHT_DELTA > m_options.nBlockMaxWeight) {
// Give up if we're close to full and haven't succeeded in a while
break;
}
Why this scored 33/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.