Skip to content

resetGasMetering() corrupts the state of memory-allocated arrays during fuzz testing #12474

@pbl64k

Description

@pbl64k

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge Version: 1.4.4-stable Commit SHA: 0579449 Build Timestamp: 2025-11-03T23:44:21.031788094Z (1762213461) Build Profile: maxperf

What version of Foundryup are you on?

foundryup: 1.1.0

What command(s) is the bug in?

forge test

Operating System

Linux

Describe the bug

Minimized repro:

// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.29;

import { Test, console } from "forge-std/Test.sol";

contract ReproTest is Test {
    function testFuzz(
        uint8[1] memory x
    ) public {
        uint8[1] memory z;
        z[0] = x[0];
        assertEq(z[0], x[0]);
        vm.resetGasMetering();
        assertEq(x[0], z[0]);
    }
}

Expected behavior: forge test -vvvv Repro passes.

Observed behavior:

$ forge test -vvvv Repro
[⠊] Compiling...
No files changed, compilation skipped

Ran 1 test for test/ReproTest.sol:ReproTest
[FAIL: assertion failed: 248 != 0; counterexample: calldata=0xbe3405cc00000000000000000000000000000000000000000000000000000000000000f8 args=[[248]]] testFuzz(uint8[1]) (runs: 0, μ: 0, ~: 0)
Traces:
  [789] ReproTest::testFuzz([248])
    ├─ [0] VM::resetGasMetering()
    │   └─ ← [Return]
    ├─ [0] VM::assertEq(248, 0) [staticcall]
    │   └─ ← [Revert] assertion failed: 248 != 0
    └─ ← [Revert] assertion failed: 248 != 0

Backtrace:
  at VM.assertEq
  at ReproTest.testFuzz

Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 1.36ms (1.06ms CPU time)

Ran 1 test suite in 19.67ms (1.36ms CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)

Failing tests:
Encountered 1 failing test in test/ReproTest.sol:ReproTest
[FAIL: assertion failed: 248 != 0; counterexample: calldata=0xbe3405cc00000000000000000000000000000000000000000000000000000000000000f8 args=[[248]]] testFuzz(uint8[1]) (runs: 0, μ: 0, ~: 0)

Encountered a total of 1 failing tests, 0 tests succeeded

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions