Skip to content

🐞 Fix Base58 decodeWord sanitizer ordering#1544

Open
cristianizzo wants to merge 1 commit into
Vectorized:mainfrom
cristianizzo:fix/base58-decodeword-sanitizer-ordering
Open

🐞 Fix Base58 decodeWord sanitizer ordering#1544
cristianizzo wants to merge 1 commit into
Vectorized:mainfrom
cristianizzo:fix/base58-decodeword-sanitizer-ordering

Conversation

@cristianizzo

Copy link
Copy Markdown

Fixes #1543.

Base58.decodeWord performed mload(c) (the lookup-table read) before validating that the input character is a valid Base58 character. For an input byte below '1' (0x31) — e.g. '0' (0x30) — the index c = byte - 49 underflows to ~2²⁵⁶, so mload(c) expands memory past the block gas limit and the call reverts with out-of-gas instead of a clean Base58DecodingError.

This mirrors the ordering already used in decode: validate the character with the shl(c, 1) bitmask check before mload(c), then perform the multiplication/addition overflow check.

Test

Added testDecodeWordLowCharacterReverts, which asserts decodeWord("0") (and a \x00 byte) now revert with Base58DecodingError. It fails on the current code (the call OOGs, reverting without the selector) and passes with this fix. The existing differential, overflow, and fuzz tests continue to pass under both the default and --via-ir profiles.

`decodeWord` performed `mload(c)` before validating the character, so an
input byte below `'1'` (0x31) underflowed the lookup index and expanded
memory, reverting with out-of-gas instead of a clean `Base58DecodingError`.
Validate the character before the load (as `decode` already does), then
perform the multiplication/addition overflow check.
Copilot AI review requested due to automatic review settings July 25, 2026 23:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes a sanitizer ordering bug in Base58.decodeWord where an out-of-bounds lookup-table read (mload(c)) could occur before validating the input character, causing pathological memory expansion and out-of-gas instead of reverting with Base58DecodingError.

Changes:

  • Reorders decodeWord validation to check the Base58 character bitmask before the lookup-table read.
  • Separates character validation from overflow checks for clearer failure modes.
  • Adds a regression test asserting low ASCII bytes (e.g. "0" and 0x00) revert cleanly with Base58DecodingError.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/utils/Base58.sol Validates the character before table lookup in decodeWord to prevent OOG on underflowed indices.
test/Base58.t.sol Adds a regression test ensuring low-character inputs revert with Base58DecodingError.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

🐞 Incorrect Sanitizer Ordering for Base58 decodeWord

2 participants