When accessing an out-of-bounds index in a fixed-size array, the contract execution reverts
- Dominant language
- C++
- Stars
- 25.7k
- Forks
- 6.2k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 29
Description
When accessing an out-of-bounds index in a fixed-size array, the contract execution reverts. However, the error message provided is a generic revert message that doesn't specify the exact cause of the error.
## Reproducible Example
pragma solidity ^0.8.26;
contract Array {
uint [6] public arr = [1,2,3];
function insertElement(uint index, uint value) public {
arr[index]= value;
}
function returnAllElement() public view returns (uint [6] memory) {
return arr;
}
function returnElement(uint index) public view returns (uint) {
return arr[index];
}
}
## Steps to Reproduce
Deploy the above contract
Now attempt to access an index that is out of bounds, e.g. arr[8]
Call returnElement(6) or any index >= 6
Current Error Message
call to Array.returnElement errored: Error occurred: revert.
revert
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
You may want to cautiously increase the gas limit if the transaction went out of gas.
Expected Behavior
The error message should clearly indicate that the index is out of bounds for the array
## Actual Behavior
The transaction is reverted with a misleading error message that mentions payable functions and gas limits
Suggested Improvement
Consider updating the error message to clearly state "Array index out of bounds" or similar.
## Environment
Compiler version: 0.8.26
Framework/IDE : Remix
EVM execution environment / backend / blockchain client:
Operating system: mac OS Somoa 14.3.1 (23D60)
Contributor guide
Research direction
Start by reproducing the Solidity 0.8.26 example in Remix and calling returnElement(6), then trace where the generic revert message is produced. Done means an out-of-bounds fixed-size array access reports that specific cause instead of the generic revert, payable, and gas-limit guidance.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, solidity
- Domain
- blockchain, compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100