10 Commits

Author SHA1 Message Date
merge-script
b62abc7eec
Merge bitcoin/bitcoin#34436: refactor: add overflow-safe CeilDiv helper and use it in unsigned callsites
02d047fd5b93d96f159db2b8e95fc39450505159 refactor: add overflow-safe `CeilDiv` helper (Lőrinc)

Pull request description:

  ### Problem
  The codebase has many open-coded ceiling-division expressions (for example `(x+y-1)/y`) scattered across files.
  These are less readable, duplicate logic, and can be overflow-prone in edge cases.

  ### Fix
  Introduce a small overflow-safe integer helper, `CeilDiv()`, and use it in existing **unsigned** callsites where the conversion is straightforward and noise-free.

  ### What this PR does
  * Adds `CeilDiv()` to `src/util/overflow.h` for unsigned integral inputs.
  * Keeps the precondition check `assert(divisor > 0)`.
  * Replaces selected unsigned ceiling-division expressions with `CeilDiv(...)`.
  * Adds focused unit tests in `src/test/util_tests.cpp` for the migrated patterns.

  ---

  This is a pure refactor with no intended behavioral change.
  Signed arithmetic callsites are intentionally left unchanged in this PR.
  This PR changed a few more things originally but based on feedback reverted to the simplest cases only.

ACKs for top commit:
  rustaceanrob:
    ACK 02d047fd5b93d96f159db2b8e95fc39450505159
  hodlinator:
    ACK 02d047fd5b93d96f159db2b8e95fc39450505159
  sedited:
    ACK 02d047fd5b93d96f159db2b8e95fc39450505159

Tree-SHA512: b09336031f487e6ce289822e0ffeb8cfc8cfe8a2f4f3f49470748dfbd0a6cbab97498674cb8686dd2bd4ab6dd0b79cfdf2da00041fee12d109892e1bc5dde0ff
2026-03-11 11:30:42 +01:00
Lőrinc
b8fa6f0f70
util: introduce TrySub to prevent unsigned underflow
Introduce `TrySub(T&, U)` which subtracts an unsigned integral `U` from an unsigned integral `T`, returning `false` on underflow.
Use with `Assume(TrySub(...))` at coins cache accounting decrement sites so invariant violations fail immediately rather than silently wrapping.

Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
Co-authored-by: Pieter Wuille <pieter@wuille.net>
2026-02-23 15:56:25 +01:00
Lőrinc
02d047fd5b
refactor: add overflow-safe CeilDiv helper
Introduce `CeilDiv()` for integral ceiling division without the typical `(dividend + divisor - 1) / divisor` overflow, asserting a non-zero divisor.

Replace existing ceiling-division expressions with `CeilDiv()` to centralize the preconditions.

Add unit tests covering return type deduction, max-value behavior, and divisor checks.
2026-02-11 18:18:21 +01:00
MarcoFalke
fa5f297748
scripted-diff: [doc] Unify stale copyright headers
-BEGIN VERIFY SCRIPT-

 sed --in-place --regexp-extended \
   's;( 20[0-2][0-9])(-20[0-2][0-9])? The Bitcoin Core developers;\1-present The Bitcoin Core developers;g' \
   $( git grep -l 'The Bitcoin Core developers' -- ':(exclude)COPYING' ':(exclude)src/ipc/libmultiprocess' ':(exclude)src/minisketch' )

-END VERIFY SCRIPT-
2025-12-16 22:21:15 +01:00
Lőrinc
ab2b67fce2 scripted-diff: modernize outdated trait patterns - values
See https://en.cppreference.com/w/cpp/types/is_enum for more details.

-BEGIN VERIFY SCRIPT-
sed -i -E 's/(std::[a-z_]+)(<[^<>]+>)::value\b/\1_v\2/g' $(git grep -l '::value' ./src ':(exclude)src/bench/nanobench.h' ':(exclude)src/minisketch' ':(exclude)src/span.h')
-END VERIFY SCRIPT-
2025-02-21 10:43:01 +01:00
TheCharlatan
c03a2795a8
util: Add integer left shift helpers
The helpers are used in the following commits to increase the safety of
conversions during cache size calculations.

Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
Co-authored-by: stickies-v <stickies-v@protonmail.com>
2025-01-15 15:43:05 +01:00
Hennadii Stepanov
306ccd4927
scripted-diff: Bump copyright headers
-BEGIN VERIFY SCRIPT-
./contrib/devtools/copyright_header.py update ./
-END VERIFY SCRIPT-

Commits of previous years:
- 2021: f47dda2c58b5d8d623e0e7ff4e74bc352dfa83d7
- 2020: fa0074e2d82928016a43ca408717154a1c70a4db
- 2019: aaaaad6ac95b402fe18d019d67897ced6b316ee0
2022-12-24 23:49:50 +00:00
MarcoFalke
faa7d8a3f7
util: Add SaturatingAdd helper 2022-02-21 14:32:53 +01:00
MarcoFalke
fab02f7991
streams: Fix read-past-the-end and integer overflows 2022-02-09 17:20:22 +01:00
MarcoFalke
fac01888d1
Move AdditionOverflow to util, Add CheckedAdd with unit tests 2021-12-17 10:46:39 +01:00