Skip to content

Commit

Permalink
fix: better error for non-hex-prefixed hex strings
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Nov 9, 2023
1 parent 6e3d88b commit 007d3d1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
39 changes: 38 additions & 1 deletion crates/cheatcodes/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,42 @@ where
}

fn parse_value(s: &str, ty: &DynSolType) -> Result<DynSolValue> {
ty.coerce_str(s).map_err(|e| fmt_err!("failed parsing {s:?} as type `{ty}`: {e}"))
match ty.coerce_str(s) {
Ok(value) => Ok(value),
Err(e) => {
let base_err = format!("failed parsing {s:?} as type `{ty}`: ");
match parse_value_fallback(s, ty) {
Some(Ok(value)) => Ok(value),
Some(Err(e2)) => Err(fmt_err!("{base_err}{e2}")),
None => Err(fmt_err!("{base_err}{e}")),
}
}
}
}

// More lenient parsers than `coerce_str`.
fn parse_value_fallback(s: &str, ty: &DynSolType) -> Option<Result<DynSolValue, &'static str>> {
match ty {
DynSolType::Bool => {
let b = match s {
"1" => true,
"0" => false,
s if s.eq_ignore_ascii_case("true") => true,
s if s.eq_ignore_ascii_case("false") => false,
_ => return None,
};
return Some(Ok(DynSolValue::Bool(b)));
}
DynSolType::Int(_) |
DynSolType::Uint(_) |
DynSolType::FixedBytes(_) |
DynSolType::Bytes => {
if !s.starts_with("0x") && s.chars().all(|c| c.is_ascii_hexdigit()) {
return Some(Err("missing hex prefix (\"0x\") for hex string"))
}
}
DynSolType::String => return Some(Ok(DynSolValue::String(s.to_owned()))),
_ => {}
}
None
}
6 changes: 6 additions & 0 deletions crates/forge/tests/it/repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ async fn test_issue_5808() {
test_repro!("Issue5808");
}

// <https://github.com/foundry-rs/foundry/issues/6070>
#[tokio::test(flavor = "multi_thread")]
async fn test_issue_6070() {
test_repro!("Issue6070");
}

// <https://github.com/foundry-rs/foundry/issues/6115>
#[tokio::test(flavor = "multi_thread")]
async fn test_issue_6115() {
Expand Down
7 changes: 3 additions & 4 deletions testdata/repros/Issue5808.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ contract Issue5808Test is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function testReadInt() public {
// TODO: blocked on https://github.com/alloy-rs/core/issues/387
// string memory str1 = '["ffffffff","00000010"]';
// vm.expectRevert();
// int256[] memory ints1 = vm.parseJsonIntArray(str1, "");
string memory str1 = '["ffffffff","00000010"]';
vm.expectRevert();
int256[] memory ints1 = vm.parseJsonIntArray(str1, "");

string memory str2 = '["0xffffffff","0x00000010"]';
int256[] memory ints2 = vm.parseJsonIntArray(str2, "");
Expand Down
16 changes: 16 additions & 0 deletions testdata/repros/Issue6070.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

import "ds-test/test.sol";
import "../cheats/Vm.sol";

// https://github.com/foundry-rs/foundry/issues/6070
contract Issue6066Test is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function testNonPrefixed() public {
vm.setEnv("__FOUNDRY_ISSUE_6066", "abcd");
vm.expectRevert("failed parsing \"abcd\" as type `uint256`: missing hex prefix (\"0x\") for hex string");
uint256 x = vm.envUint("__FOUNDRY_ISSUE_6066");
}
}

0 comments on commit 007d3d1

Please sign in to comment.