Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index out of bounds: the len is 0 but the index is 0 #4630

Closed
2 tasks done
fergarrui opened this issue Mar 23, 2023 · 5 comments · Fixed by #4631
Closed
2 tasks done

index out of bounds: the len is 0 but the index is 0 #4630

fergarrui opened this issue Mar 23, 2023 · 5 comments · Fixed by #4631
Labels
T-bug Type: bug

Comments

@fergarrui
Copy link

Component

Forge

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

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (6570a4c 2023-03-23T00:04:51.797681082Z)

What command(s) is the bug in?

forge script

Operating System

Linux

Describe the bug

When using vm.parseJsonUint on a json where the key is not found I'm getting:

The application panicked (crashed).
Message:  index out of bounds: the len is 0 but the index is 0
Location: evm/src/executor/inspector/cheatcodes/ext.rs:424

This is a bug. Consider reporting it at https://github.com/foundry-rs/foundry

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   1: __libc_start_main<unknown>
      at /build/glibc-sMfBJT/glibc-2.31/csu/../csu/libc-start.c:308

It happened to me having a JSON like:

{
  "local": {
    "prop1": 10,
  }
}

Used to work in previous versions:

vm.parseJsonUint(configJson, "local.prop1")

With latest I get this error:

Error: 
path error: 
$local.prop1
^^^^^^

Then I saw that jq syntax is required now, so when I do (it starts with a dot . now):

vm.parseJsonUint(configJson, ".local.prop1")

Is when I get the crash.
In my use case, I check if prop1 exists in some objects in the JSON, but when it is not found is when it crashes.

@mattsse
Copy link
Member

mattsse commented Mar 23, 2023

I was unable to reproduce this

       function test_repro() public {
        string memory path = "./Issue4630.json";
        string memory json = vm.readFile(path);
        uint256 val = vm.parseJsonUint(json, ".local.prop1");
        console.log(val);
    }

with

{
    "local": {
        "prop1": 10
    }
}

@mattsse
Copy link
Member

mattsse commented Mar 23, 2023

but there's a panic if the object is empty, so I'll fix that:

{
    "local": {}
}

@fergarrui
Copy link
Author

@mattsse Yes that's right sorry the issue happens when the property is not found.

Thanks!

@karmacoma-eth
Copy link
Contributor

karmacoma-eth commented Mar 24, 2023

Wanted to confirm this, here is an alternative repro:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import {stdJson} from "forge-std/StdJson.sol";

import "forge-std/Test.sol";

contract JsonTest is Test {
    function testJson() public {
        string memory json = '{"name":"John","age":30,"city":"New York"}';

        // works
        assertEq(stdJson.readString(json, ".name"), "John");

        // crashes with
        //  The application panicked (crashed).
        //  Message:  index out of bounds: the len is 0 but the index is 0
        //  Location: evm/src/executor/inspector/cheatcodes/ext.rs:424
        stdJson.readString(json, ".darkest_secret");
    }
}

Running a git bisect points at f8e700 being the first bad commit where this started happening

@mattsse
Copy link
Member

mattsse commented Mar 24, 2023

should be fixed via #4631

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-bug Type: bug
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants