Skip to content

fix: checked arithmetic for RPC data decoding#161

Open
jxom wants to merge 1 commit intomainfrom
fix/checked-arithmetic
Open

fix: checked arithmetic for RPC data decoding#161
jxom wants to merge 1 commit intomainfrom
fix/checked-arithmetic

Conversation

@jxom
Copy link
Copy Markdown
Member

@jxom jxom commented Mar 31, 2026

Summary

Fix four High severity audit findings related to unchecked arithmetic in RPC data decoding.

Changes

  1. Timestamp overflow in decode_blocktimestamp_secs * 1000 overflows for large timestamps. Now uses checked_mul with i64::try_from validation, returning an error instead of silently wrapping.

  2. Log index narrowing in decode_loglog_index and transaction_index were cast with unchecked as i32. Now uses i32::try_from() and returns error on overflow.

  3. Receipt index narrowing in decode_receipttransaction_index was cast with unchecked as i32. Now uses i32::try_from().

  4. u64::MAX overflow in tick_realtimecurrent_to + 1 and tip_num + 1 could overflow when remote_head is u64::MAX. Now uses saturating_add.

All three decode functions now return Result, propagating errors to callers throughout engine.rs.

Tests

  • decode_block_timestamp_overflow_returns_error — verifies u64::MAX timestamp returns error
  • decode_block_normal_timestamp_succeeds — verifies normal timestamps decode correctly
  • decode_log_index_overflow_returns_error — verifies u64::MAX log index returns error
  • decode_log_tx_index_overflow_returns_error — verifies u64::MAX tx index returns error
  • decode_log_normal_values_succeeds — verifies normal log values decode correctly
  • test_saturating_add_prevents_u64_max_overflow — verifies saturating arithmetic at u64::MAX

Fix four High severity audit findings related to unchecked arithmetic:

1. Timestamp overflow in decode_block: timestamp_secs * 1000 can overflow
   for large timestamps. Now uses checked_mul with i64::try_from validation.

2. Log index narrowing in decode_log: log_index and transaction_index were
   cast with unchecked `as i32`. Now uses i32::try_from() and returns error
   on overflow.

3. Receipt index narrowing in decode_receipt: transaction_index was cast
   with unchecked `as i32`. Now uses i32::try_from().

4. eth_blockNumber u64::MAX overflow in tick_realtime: current_to + 1 and
   tip_num + 1 could overflow at u64::MAX. Now uses saturating_add.

All three decode functions now return Result, propagating errors to callers.
Added unit tests for overflow cases and normal value decoding.

Amp-Thread-ID: https://ampcode.com/threads/T-019d458d-01b9-76ca-9fb1-b40dd6c6a615
Co-authored-by: Amp <amp@ampcode.com>
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.

1 participant