Skip to content

fix(lib): use int64 for mempool bytes counter to prevent overflow - #594

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

fix(lib): use int64 for mempool bytes counter to prevent overflow#594
yasinlex wants to merge 1 commit into
canopy-network:mainfrom
yasinlex:fix/integer-overflow-in-mempool

Conversation

@yasinlex

@yasinlex yasinlex commented Sep 5, 2026

Copy link
Copy Markdown

Problem

The mempool's txsBytes counter used int type, which is 32 bits on 32-bit systems. This could overflow when accumulating large transactions, potentially causing:

  1. Incorrect mempool size tracking
  2. Failure to enforce MaxTotalBytes limit
  3. Unexpected behavior in transaction dropping logic

Changes

// Before
type FeeMempool struct {
    pool     MempoolTxs
    txsBytes int           // 32-bit on 32-bit systems
    config   MempoolConfig
}

// After
type FeeMempool struct {
    pool     MempoolTxs
    txsBytes int64         // Always 64-bit
    config   MempoolConfig
}

Updated all internal calculations to use int64:

  • AddTransactions(): local txsBytes variable
  • DeleteTransaction(): convert deleted bytes to int64
  • Drop loop: convert dropped bytes to int64

The TxsBytes() method still returns int for backward compatibility with the Mempool interface.

Impact

  • Before: Potential integer overflow on 32-bit systems with >2GB of transactions
  • After: Safe handling of large transaction volumes up to 9.2 exabytes

Changed the internal bytes counter in FeeMempool from int to int64
to prevent potential integer overflow on 32-bit systems when the
mempool accumulates a large number of transactions.

Changes:
- FeeMempool.txsBytes: int -> int64
- AddTransactions(): local txsBytes variable: int -> int64
- DeleteTransaction(): convert deletedBz to int64
- TxsBytes(): convert int64 back to int for interface compatibility

This ensures the mempool can handle large transaction volumes without
risking integer 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