foundry-rs / foundry-rs/forge-std

bug(Vm): setArbitraryStorage slot set to 0 reverts to arbitrary in test

Open
#667 0 comments 0 reactions 0 assignees View on GitHub
bug dependent
Dominant language
Solidity
Stars
1.1k
Forks
520
Avg merge
2d 21h
Merged PRs (30d)
11

Description

I'm trying to write a `setUp` function that first uses the `setArbitraryStorage` cheatcode to make the storage of a contract arbitrary, then uses `store` to set a single storage slot to 0. This seems to work inside the `setUp`, but when the test itself is executed, the value of the storage slot goes back to being a random number. This seems to only happen when the value is 0.

Forge version:

```
forge Version: 1.0.0-stable
Commit SHA: e144b82070619b6e10485c38734b4d4d45aebe04
Build Timestamp: 2025-02-13T20:03:31.026474817Z (1739477011)
Build Profile: maxperf
```

## Minimal Example

```solidity
pragma solidity ^0.8.25;

import "../lib/forge-std/src/Test.sol";

contract StorageContract {
uint256 public value;
}

contract ArbitraryStorageTest is Test {
uint256 constant DEFAULT_VALUE = 0;

StorageContract target;

function setUp() public {
target = new StorageContract();
vm.setArbitraryStorage(address(target));

bytes32 valueSlot = bytes32(uint256(0));
vm.store(address(target), valueSlot, bytes32(DEFAULT_VALUE));
emit log_named_uint("value in setUp", target.value());
}

function test_stored_value() public {
emit log_named_uint("value in test", target.value());

assertEq(target.value(), DEFAULT_VALUE);
}
}
```
The test fails and this is what the logs show:
```
[FAIL: assertion failed: 36440280036344177981963967840477496355477405577495123809133350823800994840471 != 0] test_stored_value() (gas: 10093)
Logs:
value in setUp: 0
value in test: 36440280036344177981963967840477496355477405577495123809133350823800994840471
```
However, if `DEFAULT_VALUE` is changed to 1, the result is correct:
```
[PASS] test_stored_value() (gas: 10090)
Logs:
value in setUp: 1
value in test: 1
```

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.