Elements of `bytes constant` are not read-only
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 21
Description
## 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
```solidity
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:
```solidity
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;
}
}
```
Contributor guide
Research direction
Start by compiling the Solidity reproduction and compare the diagnostics for bytes constant with the existing fixed-bytes behavior. Trace the compiler path handling element assignment and delete for constant dynamic bytes values; done means both mutations are rejected while reading and returning B remains valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, solidity
- Domain
- blockchain, compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100