Skip to content

net: prevent signed integer overflow in AddLocal nScore update - #34674

Closed
chriszeng1010 wants to merge 1 commit into
bitcoin:masterfrom
chriszeng1010:fix-addlocal-nscore-overflow
Closed

chriszeng1010 wants to merge 1 commit into
bitcoin:masterfrom
chriszeng1010:fix-addlocal-nscore-overflow

Conversation

@chriszeng1010

Copy link
Copy Markdown

When AddLocal() is called for an address already in mapLocalHost, it increments nScore by 1. If nScore is at INT_MAX, this is signed integer overflow (undefined behavior).

Use SaturatingAdd from util/overflow.h to cap at INT_MAX instead.

This is the same class of bug addressed for SeenLocal() in #34028, but in a different call site.

@DrahtBot DrahtBot added the P2P label Feb 25, 2026
@DrahtBot

DrahtBot commented Feb 25, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Comment thread src/net.cpp
LocalServiceInfo &info = it->second;
if (is_newly_added || nScore >= info.nScore) {
info.nScore = nScore + (is_newly_added ? 0 : 1);
info.nScore = is_newly_added ? nScore : SaturatingAdd(nScore, 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there realistic scenarios where nScore equals INT_MAX here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a consistency fix. The same overflow pattern was already fixed in SeenLocal() via #34028, nScore would need billions of AddLocal() calls on the same address.

@maflcko

maflcko commented Feb 27, 2026

Copy link
Copy Markdown
Member

Was this LLM generated? What are the steps to test this? What is the output before and after the changes here?

@chriszeng1010

Copy link
Copy Markdown
Author

@maflcko I used an LLM to help identify this. I noticed the prior fix in #34028 for SeenLocal() and checked whether the same pattern existed in AddLocal() — it does

@maflcko maflcko closed this Feb 27, 2026
@maflcko

maflcko commented Feb 27, 2026

Copy link
Copy Markdown
Member

Thx, but LLM output is not accepted if the author can not properly explain, test or could have written the change themselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants