Skip to content

fix(lib): prevent integer overflow in AddToPort - #595

Open
yasinlex wants to merge 1 commit into
canopy-network:mainfrom
yasinlex:fix/integer-overflow-in-addtoport
Open

yasinlex wants to merge 1 commit into
canopy-network:mainfrom
yasinlex:fix/integer-overflow-in-addtoport

Conversation

@yasinlex

@yasinlex yasinlex commented Sep 5, 2026

Copy link
Copy Markdown

Problem

AddToPort() converts a uint64 value to int without checking for overflow:

newPort := port + int(add)  // int(add) can overflow!

If add is very large (close to math.MaxUint64), int(add) would overflow to a negative number, causing the subsequent bounds check to fail.

Fix

Added overflow check before the conversion:

if add > math.MaxInt64 {
    return "", ErrMaxPort()
}

Impact

  • Before: Large add value → integer overflow → incorrect port calculation
  • After: Large add value → returns ErrMaxPort() error

Added overflow check before converting uint64 add value to int.
Previously, if add was very large (close to math.MaxUint64), the
conversion to int could overflow, causing the subsequent bounds
check to fail.

Now checks if add > math.MaxInt64 before conversion and returns
ErrMaxPort() if it would overflow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants