Skip to content

Conversation

@ep0chzer0
Copy link
Contributor

Summary

Fixes slither-read-storage failing on user-defined value types (TypeAlias) like type MyUint64 is uint64 inside structs.

Problem

When a struct contains custom types, read_storage fails with:

UnboundLocalError: local variable 'size' referenced before assignment

This happens because _find_struct_var_slot only handles ElementaryType, not TypeAlias.

Solution

  • Import TypeAlias from slither.core.solidity_types
  • Handle TypeAlias at the top-level storage variable type check in get_storage_slot
  • Unwrap TypeAlias to its underlying ElementaryType in _find_struct_var_slot

Test Case (from issue)

type MyUint64 is uint64;
contract C {
    struct S {
        MyUint64 y;
        MyUint64 z;
    }
    S s;
}

This now works correctly instead of raising an error.

Fixes #2522

🤖 Generated with Claude Code

@ep0chzer0 ep0chzer0 requested a review from smonicas as a code owner January 16, 2026 12:04
@ep0chzer0
Copy link
Contributor Author

Hi maintainers, could you please approve the CI workflow runs for this PR? As a first-time contributor, the workflows require maintainer approval. Thank you!

@ep0chzer0 ep0chzer0 force-pushed the fix/read-storage-type-alias branch 3 times, most recently from e705382 to a62d36c Compare January 16, 2026 23:28
Copy link
Member

@dguido dguido left a comment

Choose a reason for hiding this comment

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

Code Review: fix: handle TypeAlias in read_storage for custom value types

Reviewing: PR #2911 (fix/read-storage-type-alias branch)
Files changed: slither/tools/read_storage/read_storage.py


Summary

This PR fixes an UnboundLocalError when slither-read-storage encounters user-defined value types (e.g., type MyUint64 is uint64) inside structs. The fix correctly imports TypeAlias and unwraps it to its underlying ElementaryType in two key locations.


Analysis

The fix is correct and addresses the root cause. The changes:

  1. Import TypeAlias - Required to check for the type.

  2. Handle TypeAlias in get_storage_slot (lines 245-248 in the new code) - Handles top-level storage variables that are TypeAlias directly.

  3. Unwrap TypeAlias in _find_struct_var_slot (lines 596-598 in the new code) - This is the critical fix. The original code only handled ElementaryType, causing size to remain uninitialized when iterating over struct members that are TypeAlias.

Why the fix works:

  • TypeAlias.underlying_type returns the wrapped ElementaryType (e.g., uint64)
  • After unwrapping, the existing ElementaryType handling code correctly computes size from var_type.size

Issues Found

No critical or important issues (confidence >= 80).

The implementation is correct and follows patterns already established in the codebase.


Minor Observations (below reporting threshold)

  1. Formatting-only changes - The PR includes several formatting changes (multi-line argument formatting) that appear to be from ruff. These are fine but inflate the diff.

  2. Test coverage - The PR lacks a dedicated test case for TypeAlias handling. The existing test contracts (StorageLayout.sol) don't use user-defined value types. Consider adding a test in a follow-up PR to prevent regression.

  3. Other TypeAlias locations - The _find_array_slot and _find_mapping_slot methods might also encounter TypeAlias types in nested structures. However, these paths eventually call _find_struct_var_slot, which now handles unwrapping correctly. The current fix should cover these cases transitively.


Verdict

Approve. The fix is minimal, correct, and directly addresses the reported issue (#2522). The code follows existing patterns in the file.

Suggestion for maintainers: Consider adding a test case with user-defined value types to the read-storage test suite to prevent regression.

dguido added a commit to ep0chzer0/slither that referenced this pull request Jan 20, 2026
Apply PR crytic#2911 changes to fix slither-read-storage failing on
user-defined value types (TypeAlias) like `type MyUint64 is uint64`
inside structs.

- Import TypeAlias from slither.core.solidity_types
- Handle TypeAlias at top-level storage variable type check in get_storage_slot
- Unwrap TypeAlias to its underlying ElementaryType in _find_struct_var_slot

Fixes crytic#2522

Co-Authored-By: ep0chzer0 <[email protected]>
Co-Authored-By: Claude Opus 4.5 <[email protected]>
@dguido dguido force-pushed the fix/read-storage-type-alias branch from a62d36c to d850341 Compare January 20, 2026 05:15
@dguido
Copy link
Member

dguido commented Jan 20, 2026

Bulk PR Review Summary

Research Findings

  • Issue: PR fixes ”read_storage cannot determine the storage layout of custom type variables within a struct. #2522 - slither-read-storage fails on user-defined value types (TypeAlias) like type MyUint64 is uint64 inside structs
  • Root Cause: _find_struct_var_slot only handled ElementaryType, causing an UnboundLocalError: local variable 'size' referenced before assignment when encountering TypeAlias
  • Solution: Import TypeAlias and unwrap it to its underlying ElementaryType in two locations:
    1. Top-level type check in get_storage_slot (lines 245-247)
    2. Struct member iteration in _find_struct_var_slot (lines 596-598)

Test Status

  • Unit tests: 194 passed, 14 xfailed (xfails are expected - vyper tests without vyper installed)
  • E2E detector tests: 379 passed
  • Read-storage specific tests: Require ganache (not available in this environment), but the fix is verified by code review

Code Review Issues Found/Fixed

No functional issues found. The fix is minimal, correct, and directly addresses the root cause.

Formatting changes: The PR includes ruff-driven formatting changes (multi-line argument wrapping) which are acceptable but inflate the diff from +37/-6 to appear larger.

Test coverage gap: As noted by @dguido's earlier review, the PR lacks a dedicated test case for TypeAlias handling. The existing StorageLayout.sol test contracts don't include user-defined value types. A follow-up PR adding test coverage would strengthen this fix.

Changes Made

  • Rebased onto current master (26e522429)
  • Preserved original PR changes
  • Squashed the two commits (fix + style) into one clean commit with proper attribution

Merge Readiness

Ready to merge - The fix is correct, minimal, follows existing patterns in the codebase, and all tests pass. The only suggestion is to add test coverage in a follow-up PR to prevent regression.

@ep0chzer0 ep0chzer0 force-pushed the fix/read-storage-type-alias branch 5 times, most recently from 5c20955 to fc69bf6 Compare January 22, 2026 14:49
Adds support for user-defined value types (TypeAlias) in slither-read-storage.
Previously, custom types like `type MyUint64 is uint64` inside structs would
cause an UnboundLocalError because the code only handled ElementaryType.

Changes:
- Import TypeAlias from slither.core.solidity_types
- Handle TypeAlias at top-level storage variable type check
- Unwrap TypeAlias to its underlying ElementaryType in _find_struct_var_slot

Fixes crytic#2522
@ep0chzer0 ep0chzer0 force-pushed the fix/read-storage-type-alias branch from fc69bf6 to 289d13d Compare January 23, 2026 18:03
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.

”read_storage cannot determine the storage layout of custom type variables within a struct.

2 participants