argotorg / argotorg/solidity

Slicing calldata arrays out of bounds revert without reason

Open
#14,677 1 comment 0 reactions 1 assignee Claimed by @nikola-matic View on GitHub
bug :bug:
Dominant language
C++
Stars
25.7k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
21

Description

## Description

According to [the documentation](https://docs.soliditylang.org/en/v0.8.23/control-structures.html#panic-via-assert-and-error-via-require), the `0x32` panic code corresponds to an access out of bounds in arrays.

> If you access an array, bytesN or an array slice at an out-of-bounds or negative index (i.e. x[i] where i >= x.length or i < 0).

The description makes explicit that an out-of-bounds access to an array slice should panic with this code. However, it reverts without a reason when an array slice specifies an end out of bounds (i.e.`end >= x.length`).

It's true the description only mentions _access_ to an array, but the result of an array slice is another array, so why would "array slice" be mentioned if this panic code only shows up when accessing regular arrays?

Ideally, an array slice out of bounds would revert with an `0x32` code as well.

## Environment

- Compiler version: 0.8.22
- Target EVM version (as per compiler settings): Shanghai
- Framework/IDE (e.g. Truffle or Remix): Foundry

## Steps to Reproduce

```solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;

contract OutOfBounds {
function shouldPanicWithShortCalldata(bytes memory data) public pure {
// Does panic
data[4];
}

function shouldPanicWithShortCalldataToo(bytes calldata data) public pure {
// No panic
data[:4];
}
}
```

Tests:

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.22;

import {Test, stdError} from "forge-std/Test.sol";
import "../src/OutOfBounds.sol";

contract OutOfBoundsTest is Test {
OutOfBounds public outOfBounds;
bytes data = hex"123456";

function setUp() public {
outOfBounds = new OutOfBounds();
}

function testShouldPanicWithShortCalldata() public {
vm.expectRevert(stdError.indexOOBError);
outOfBounds.shouldPanicWithShortCalldata(data);
}

function testShouldPanicWithShortCalldataToo() public {
vm.expectRevert(stdError.indexOOBError);
outOfBounds.shouldPanicWithShortCalldataToo(data);
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.