Home / Smart Contract Auditing / Critical Solidity Compiler Bug Discovered: Memory Byte Array Element Deletion Overwrites Surrounding Data

Critical Solidity Compiler Bug Discovered: Memory Byte Array Element Deletion Overwrites Surrounding Data

On August 14, 2026, security researcher Shaheen Fazim uncovered a significant vulnerability within the Solidity code generator, officially reported through the Ethereum Foundation’s bug bounty program. The defect relates to how smart contracts handle the delete operator when it is applied to specific elements of dynamic memory byte arrays. Rather than clearing solely the targeted byte, the generated bytecode erroneously executes a full 32-byte zero write starting at that exact index. This unintended memory overflow alters data further down within the same array or potentially spills over into adjacent memory allocations entirely.

Assigned a low-to-medium severity rating on the core team’s internal scale, the flaw specifically targets the default evmasm compilation pipeline. Contracts compiled using the alternative IR pipeline remain completely unaffected. Furthermore, invoking or omitting the Solidity optimizer has no impact on whether the bug triggers, as the faulty behavior stems directly from the baseline code generation stage. Because this specific code path predates the very first official release of Solidity, the vulnerability impacts all historical versions up to and including version 0.8.36. The Ethereum Foundation and core development team resolved the issue with the release of Solidity 0.8.37.

To gauge the real-world exposure of this compiler defect, developers scanned the Sourcify database, which catalogs approximately 870,000 verified smart contract compilations. The analysis revealed that the vulnerable source code pattern appeared in a mere 17 compilations, encompassing only two distinct functions. Upon closer inspection, neither of those functions was found to be actively vulnerable in practice. In both instances, the bytes overwritten inside the array were either immediately discarded or deleted anyway, while memory allocations located beyond the array boundaries did not contain critical execution data. Consequently, security assessments indicate that no deployed, production-grade smart contracts are known to have suffered malicious exploitation or unintended data corruption from this bug.

Which Contracts Are Affected?

Understanding exposure to this compiler bug requires evaluating specific runtime conditions within smart contract codebases. A contract faces potential vulnerability only when multiple distinct criteria align during compilation and execution. First, the project must be compiled using an affected version of Solidity, spanning anywhere from the earliest legacy versions up to version 0.8.36. Second, the compilation must utilize the default evmasm pipeline rather than the modern IR pipeline. Finally, the source code must explicitly apply the delete operator to an individual element of a memory-resident bytes or string data structure.

Developers can easily verify the first two conditions by reviewing their project configuration files and compiler settings. The third condition, however, requires careful analysis of the dynamic memory layout produced by the compiler. If the first two criteria match a project, security best practices suggest treating the contract as potentially affected rather than attempting to prove the safety of the memory layout manually.

It is important to emphasize that if a smart contract never applies the delete operator to an individual element of a bytes or string value residing in memory, it is entirely safe from this defect. Neither the activation of the optimizer nor the target Ethereum Virtual Machine version alters whether the bug manifests. The root cause lies entirely within the instructions emitted during the code generation phase, and the optimizer faithfully preserves those flawed semantic behaviors.

Clarifying the scope of the vulnerability involves several key distinctions. First, only individual element deletions are compromised; calling delete b on an entire array routes through a completely separate code path and functions correctly. Second, the target array must reside in memory. Byte arrays stored in persistent storage utilize an entirely different handling mechanism and remain unaffected, while the delete operator cannot be applied to calldata arrays at all. Additionally, arrays where each individual element occupies a full 32-word block in memory, such as bytes1[] memory arrays, are completely immune. Only packed structures like bytes and string compress their elements, making them susceptible.

Finally, string values share the underlying vulnerability. While direct syntax like delete s[i] does not compile for a string memory s variable, developers frequently convert strings into byte arrays via bytes(s). Executing delete bytes(s)[i] on that conversion successfully compiles and triggers the exact same memory corruption bug. The precise reach of the resulting data corruption depends heavily on the index of the deleted element and how the array was initially allocated in memory. Deleting one of the final 31 elements of an array can easily push the 32-byte zero write past the array’s boundary and into whatever data structure happens to be allocated immediately afterward.

Technical Details

A deeper examination of compiler internals reveals the technical root cause behind the vulnerability. Most data types in Solidity memory occupy full 32-byte words. The major exception is the bytes type, where a memory-resident byte array consists of an initial 32-byte length descriptor followed immediately by its raw data packed tightly back-to-back without internal padding gaps.

Solidity allocates memory sequentially by advancing a global free memory pointer, meaning every new allocation begins precisely where the previous one terminated. Consecutive allocations sit flush against one another, separated only by optional padding. As a result, any write operation that runs past the intended end of a data structure spills directly into the next adjacent block of memory.

For most standard allocations, such as arrays initialized via new bytes(n) or instantiated directly from literals, the raw data is followed by up to 31 zero bytes of padding. This ensures that any subsequent memory allocation begins cleanly on a proper 32-byte word boundary. These are classified as padded arrays. Conversely, the legacy evmasm pipeline leaves certain allocations completely unpadded, specifically the outputs of bytes.concat, string.concat, and the entire family of abi.encode functions. The data for these unpadded arrays ends right where the next allocation starts. Whether an array is padded is an internal compiler detail that can vary between pipelines, and code must never rely on consistent padding behaviors.

Memory Byte Array Element Delete Clears Whole Word Bug | Solidity Programming Language

When memory values are narrower than a full 32-byte word, their positioning depends strictly on their data type. Left-aligned values, such as fixed-size bytesN types, anchor themselves at the very beginning of the word. Right-aligned values, including standard integers, addresses, memory pointers, and dynamic array length headers, sit at the far end of the word with leading zero bytes filling the front. Because their elements consist of single bytes, dynamic byte arrays represent a unique scenario where storing a value to memory diverges from writing a full word. The compiler normally handles this by issuing a single-byte store instruction (mstore8) wherever it tracks element widths, with one critical exception occurring along the delete code path.

The Delete Path

The fundamental purpose of delete x is to assign the default zero value of that variable’s type, meaning delete b[i] should behave identically to an explicit assignment like b[i] = 0. Both operations ultimately conclude by storing zero to the target element’s memory location. However, within the default evmasm compilation pipeline, these two operations do not share the same underlying store implementation.

When executing a standard element assignment like b[i] = 0, the compiler correctly resolves the element width and emits a targeted single-byte store (mstore8), updating only the single byte at index i. When executing the delete operator on that same element, the compiler fails to check the element width. Instead, it mistakenly triggers a full 32-byte word store (mstore).

This results in a massive 32-byte block of zeroes being written directly to memory, starting at the location of the deleted element and extending forward. Because this write initiates at the specific byte offset of the element rather than aligning with a natural word boundary, it stretches far beyond the word containing the targeted element. The array’s length descriptor, which resides before the data payload, remains untouched, allowing the array to retain its original length indicator. Consequently, any subsequent operations relying on the array’s contents—such as computing cryptographic hashes via keccak256, performing ABI encodings, or returning data to an external caller—silently process the corrupted byte sequence. The modern IR pipeline correctly utilizes a uniform store mechanism for both assignments and deletions, remaining completely immune to the defect.

Example

Demonstrating the practical implications of this vulnerability involves a simple smart contract structured to exploit adjacent memory allocations:

contract C 
    struct S  uint256 x; 

    // Corruption inside the array.
    function clearElement() public pure returns (bytes memory) 
        bytes memory d = hex"0102030405";
        delete d[0];
        return d;
    

    // Corruption beyond the array.
    function clearLastElement() public pure returns (uint256) 
        bytes memory b = new bytes(32);
        S memory s = S(type(uint256).max);
        delete b[31];
        return s.x;
    

When compiled using default settings via the evmasm pipeline, these functions yield unexpected, corrupted results. In the clearElement function, the small five-byte array occupies a portion of a single padded word. Executing the delete operation clears the entire underlying word from index zero onward, wiping out the remaining four bytes of the array alongside the target.

In the clearLastElement function, the 32-byte array occupies precisely one word without trailing padding. Deleting the final element writes 31 zero bytes directly into the memory space occupied by structure s, which sits immediately adjacent. Because integer types store their most significant byte first, the 31 overwritten bytes destroy the most significant portions of type(uint256).max, leaving only the lowest byte intact and causing the function to return a value of 255 instead of the maximum possible integer. Replacing these delete statements with standard assignments like d[0] = 0 and b[31] = 0 produces the correct, expected outputs across all compiler versions.

Severity Assessment and Recommended Actions

The Solidity compiler emits no warnings when this flawed pattern is compiled, and the generated bytecode does not trigger runtime reverts. Instead, the defect manifests as silent data corruption, complicating debugging efforts. While internal array corruption is more frequent, it can generally be caught by comprehensive test suites exercising the evmasm pipeline.

Real-world risk remains exceptionally low. The bug persisted unobserved throughout the history of the language, and ecosystem scans confirmed that vulnerable patterns appeared in only two deployed functions, neither of which suffered actual exploitation due to how their surrounding memory was utilized.

The core development team resolved the vulnerability in Solidity 0.8.37 by updating the delete code path to properly consult element widths and emit single-byte stores for packed byte arrays. Developers maintaining codebases on vulnerable compiler versions are strongly advised to upgrade their toolchains to Solidity 0.8.37 or later, or alternatively transition their compilation settings to the IR pipeline to ensure complete protection against memory corruption risks.

Tagged: