Home / Smart Contract Auditing / Solidity Vulnerability Discovered: Storage Corruption Bug in Recursive Structs Fixed in Version 0.8.39

Solidity Vulnerability Discovered: Storage Corruption Bug in Recursive Structs Fixed in Version 0.8.39

The Ethereum Foundation and the Solidity development team have officially released Solidity version 0.8.39, introducing a critical patch for a subtle and potentially severe storage corruption vulnerability. Discovered and reported by security researcher cyberthirst via the Ethereum Foundation’s official bug bounty program on April 22, 2026, the vulnerability impacts a wide range of historical compiler versions. The bug affects how the Solidity code generator handles data assignments involving recursive data structures, specifically when a nested struct or array is copied up a hierarchy onto a parent struct or array that contains it.

According to the vulnerability disclosure, the core issue stems from the way smart contracts handle storage-to-storage copy operations for recursive structs. When a descendant element is assigned to an ancestor—such as copying a node’s child back onto the root node itself—the copy operation inadvertently overwrites the source data before the compilation pipeline has finished reading it. This leads to silent data corruption, where critical variables, balances, or configuration flags are wiped out or reset to zero without throwing any compilation or runtime errors.

Understanding the Scope and Affected Versions

The discovery highlights the hidden complexities of managing memory and storage layout within Ethereum’s Virtual Machine (EVM), particularly when working with advanced data types like recursive structs. The initial bug report and corresponding proof of concept focused primarily on direct struct-to-struct copy operations. However, subsequent internal investigations by the Solidity team revealed that array-to-array copies of recursive structs were equally susceptible to the same underlying flaw.

The vulnerability affects both the traditional evmasm code generation pipeline and the newer Intermediate Representation (--via-ir) pipeline. The bug has been present in the compiler code generator since version 0.4.9, which was the first release to natively support the compilation of recursive structs. Meanwhile, the IR pipeline has been affected since version 0.8.13, marking the release where the IR compilation route was graduated from experimental status.

All Solidity compiler versions ranging from 0.4.9 through 0.8.38 are impacted by this issue. The vulnerability has been formally tracked and documented in the official Solidity list of known bugs, and developers are strongly advised to update their toolchains immediately to version 0.8.39, where the issue has been fully resolved.

Identifying Affected Smart Contracts

To determine whether a specific smart contract is vulnerable, developers must analyze how data structures are defined and manipulated within their codebase. A contract is vulnerable only if it utilizes recursive struct definitions and performs specific types of storage-to-storage assignments.

Recursive structs typically manifest when a struct contains a dynamic array of its own type, either directly or indirectly through mutual recursion involving multiple interconnected structs. Because static arrays require a fixed, predetermined size and pointers to structs are not permitted within storage structs in Solidity, dynamic arrays and mappings represent the primary architectural pathways for recursive relationships in storage. Between these two data structures, only dynamic arrays can be copied, making them the exclusive vector for this particular storage corruption bug.

The vulnerability is triggered by specific assignment patterns where a descendant node is copied onto an ancestor node, or where a nested array is copied onto an enclosing array. Crucially, the structural overlap between the source and target may not always be immediately obvious by looking at a simple assignment line, as the source data could be reached indirectly through storage pointers, mapping keys, or complex array indices.

The Solidity team has clarified that standard .push() operations cannot trigger this bug. This is because the .push() method operates in the opposite direction—appending a newly initialized element and copying the argument into it—meaning the source contains the newly created target rather than the other way around. If a contract does not utilize recursive structs within its storage layout, it remains entirely unaffected by this vulnerability.

Technical Mechanics of the Storage Overlap

The root cause of the bug lies in the unique mechanics of Ethereum storage layout and how the Solidity compiler generates bytecode to execute storage-to-storage assignments. Unlike memory or calldata, Ethereum storage relies on a deterministic, hash-based layout managed by the EVM. While value types occupy fixed storage slots, dynamic arrays occupy a single primary slot that stores the array’s length, with the actual array elements stored sequentially at locations derived from the keccak256 hash of that slot’s address.

When a storage-to-storage copy operation occurs, the compiler walks through the source struct member by member and array element by array element. In the case of a recursive struct, one of those members is a dynamic array. When the compiler executes the copy, it resizes the target array and overwrites the physical storage area that holds its elements.

In vulnerable versions, copying a child node onto a parent node meant that the empty children array belonging to the descendant was copied over the storage area belonging to the root node prematurely. This action shrank the target array and zeroed out its elements. Because the source data was still physically located within that exact storage region, the source data was cleared before the compiler finished reading all of its fields. Consequently, subsequent fields—such as numerical data or permission flags—were read back as zero, leading to silent state corruption.

The manifestation of this bug could vary depending on the internal layout of the struct fields. If an array member preceded a numerical data field, the array copy would overwrite the data field, resulting in a read value of zero. Conversely, if the data field was placed before the array, it might be read before the overwrite occurred and retain its value. However, the Solidity team emphasized that rearranging fields does not provide a reliable safeguard, as nested subtrees deeper within the structure could still be silently lost depending on runtime conditions.

Severity Assessment and Practical Impact

The Solidity team has assigned this vulnerability a medium severity rating, balancing the potential for serious consequences against a relatively low practical likelihood of occurrence in real-world deployments.

If exploited or accidentally triggered, the bug could cause critical state variables—such as user balances, ownership permissions, or administrative access controls—to be silently zeroed out during routine contract execution. This could inadvertently transform strict authorization checks into unconditional passes, potentially exposing protocols to unauthorized actions. Because the compiler raises no errors and the operations execute silently, such corruption could easily go undetected during standard testing phases.

Despite these risks, comprehensive analyses suggest that the real-world impact is minimal. Automated scans of verified smart contracts deployed across various networks, including checks performed on code repositories like Sourcify, revealed no instances of developers actively executing this specific storage-to-storage copy pattern with recursive structs. While developers occasionally use recursive tree-like structures in storage to manage hierarchical data, standard architectural practices generally favor reference-based pointer schemes or simpler mapping structures rather than moving entire heavy subtrees around in storage, due to the high gas costs involved.

The Patch and Recommended Actions

To resolve the vulnerability, the Solidity 0.8.39 update introduces an automated runtime safety check for all storage-to-storage copy operations involving recursive structs. Before executing a copy, the compiled bytecode now walks the target’s subtree to verify whether it contains the source data. If an overlap is detected, the compiler automatically creates a temporary snapshot of the source data in memory. The copy operation then safely reads from this memory snapshot, ensuring that the source and target storage locations do not interfere with one another during the write process. If no structural overlap is found, the copy proceeds directly without utilizing a memory snapshot.

This added safety measure represents a deliberate engineering trade-off. While it guarantees data integrity, the runtime check introduces additional gas overhead for storage-to-storage copies of recursive structs, with costs scaling linearly based on the depth of the target’s nested structure. Additionally, because the validation check consumes stack space during the recursive tree walk, deeply nested structures may encounter stack-to-deep errors at shallower nesting thresholds than in previous compiler versions.

The Ethereum Foundation advises developers to review their existing codebases, particularly if their smart contracts implement hierarchical tree structures or recursive data types in storage. Projects utilizing affected compiler versions should upgrade their development environments to Solidity 0.8.39 and audit their storage modification logic to ensure state integrity moving forward. The discovery and prompt remediation underscore the ongoing efficacy of the Ethereum Foundation’s bug bounty program in identifying and neutralizing subtle compiler-level risks.

Tagged:

Leave a Reply

Your email address will not be published. Required fields are marked *