Skip to content

readInt automatic base detection is a footgun #5808

@dcposch

Description

@dcposch

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 (75836a7 2023-09-09T00:31:54.458033000Z)

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

JSON reading functions readInt/readUint/readIntArray etc automatically detect the base of the string.

This leads to unpleasant surprises... in our case we had a P256 verifier working on all but four of ~300 Wycheproof test vectors, and the last four were due to readUint switching to decimal.

Minimal reproducible example

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

using stdJson for string;

contract ReadIntTest is Test {
    function testReadInt() public {
        string memory json = '["ffffffff","00000010"]';
        int256[] memory ints = json.readIntArray("");
        console2.log(ints[0]);
        console2.log(ints[1]);
    }
}
image

Proposed fix

  • Parse decimal only
  • (If there's demand for it, you could parse hex when there is an 0x prefix)

Less is more. If people pass a bare hex string to readInt, better to error right away (then they can easily uint256(json.readBytes32(...))) than sneakily, later, once some input lacks letters.

Metadata

Metadata

Assignees

Labels

T-bugType: bugfirst issueA good way to start contributing

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions