Skip to content

Commit 88c9b7f

Browse files
authored
fix(cheatcodes): fallback to string if invalid 0x hex (#8290)
1 parent 48b95f9 commit 88c9b7f

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

crates/cheatcodes/src/json.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -448,15 +448,15 @@ pub(super) fn json_value_to_token(value: &Value) -> Result<DynSolValue> {
448448
s = format!("0{val}");
449449
val = &s[..];
450450
}
451-
let bytes = hex::decode(val)?;
452-
Ok(match bytes.len() {
453-
20 => DynSolValue::Address(Address::from_slice(&bytes)),
454-
32 => DynSolValue::FixedBytes(B256::from_slice(&bytes), 32),
455-
_ => DynSolValue::Bytes(bytes),
456-
})
457-
} else {
458-
Ok(DynSolValue::String(string.to_owned()))
451+
if let Ok(bytes) = hex::decode(val) {
452+
return Ok(match bytes.len() {
453+
20 => DynSolValue::Address(Address::from_slice(&bytes)),
454+
32 => DynSolValue::FixedBytes(B256::from_slice(&bytes), 32),
455+
_ => DynSolValue::Bytes(bytes),
456+
});
457+
}
459458
}
459+
Ok(DynSolValue::String(string.to_owned()))
460460
}
461461
}
462462
}

crates/forge/tests/it/repros.rs

+3
Original file line numberDiff line numberDiff line change
@@ -343,5 +343,8 @@ test_repro!(2851, false, None, |res| {
343343
// https://github.com/foundry-rs/foundry/issues/8006
344344
test_repro!(8006);
345345

346+
// https://github.com/foundry-rs/foundry/issues/8277
347+
test_repro!(8277);
348+
346349
// https://github.com/foundry-rs/foundry/issues/8287
347350
test_repro!(8287);
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
pragma solidity 0.8.18;
3+
4+
import "ds-test/test.sol";
5+
import "cheats/Vm.sol";
6+
7+
// https://github.com/foundry-rs/foundry/issues/8277
8+
contract Issue8277Test is DSTest {
9+
Vm constant vm = Vm(HEVM_ADDRESS);
10+
11+
struct MyJson {
12+
string s;
13+
}
14+
15+
function test_hexprefixednonhexstring() public {
16+
{
17+
bytes memory b = vm.parseJson("{\"a\": \"0x834629f473876e5f0d3d9d269af3dabcb0d7d520-identifier-0\"}");
18+
MyJson memory decoded = abi.decode(b, (MyJson));
19+
assertEq(decoded.s, "0x834629f473876e5f0d3d9d269af3dabcb0d7d520-identifier-0");
20+
}
21+
22+
{
23+
bytes memory b = vm.parseJson("{\"b\": \"0xBTC\"}");
24+
MyJson memory decoded = abi.decode(b, (MyJson));
25+
assertEq(decoded.s, "0xBTC");
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)