Problem
The nScore
field in LocalServiceInfo
struct was defined as int
(32-bit signed integer), which could overflow from INT_MAX
(2,147,483,647) to INT_MIN
(-2,147,483,648) when incremented by SeenLocal()
during version handshakes. This is undefined behavior in C++ and could affect address selection logic.
Solution
Implement saturation in SeenLocal()
to cap nScore
at std::numeric_limits<int>::max()
instead of allowing overflow. This prevents undefined behavior while maintaining memory efficiency and logical correctness - after 2 billion connections, the matter is settled.
Testing
- Added test case
LocalAddress_nScore_Overflow
that verifies saturation behavior - Test passes, confirming the fix works as expected
- Backward compatible - no breaking changes
Impact
- Prevents undefined behavior from integer overflow
- Maintains memory efficiency (keeps
int
instead ofint64_t
) - Logically correct - no need for values beyond 2 billion
Fixes #24049