Elements of `bytes constant` are not read-only
Author: cameelCreated Sep 1, 2026Updated Sep 17, 2026
Labelsbug :bug:low effortlow impactmust have eventually
Description
The compiler does not disallow modifying individual bytes of bytes constant variables.
The changes do not persist, but probably only due to the fact that each use of the constant reexecutes the initializer and allocates new memory to store the value.
Environment
- Compiler version: 0.8.36
Steps to Reproduce
bytes constant B = "abc";
contract C {
function f() public returns (bytes memory) {
B[1] = "x"; // No compilation error
delete B[2]; // No compilation error
return B; // Still returns "abc"
}
}For comparison, there are compile-time checks against doing the same with a fixed-bytes variable:
bytes32 constant B32 = "abc";
contract C {
function f() public returns (bytes32) {
B32[1] = "x"; // Error: Single bytes in fixed bytes arrays cannot be modified.
delete B32[2]; // Error: Single bytes in fixed bytes arrays cannot be modified.
return B32;
}
}Source: argotorg/solidity