Skip to content

Commit 79140e9

Browse files
Fix panic on NaN/Inf values in TOML to JSON conversion (#11574)
* Update toml.rs * Update test.toml * Update Toml.t.sol * Update test.toml * Update Toml.t.sol
1 parent 37aa2ff commit 79140e9

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

crates/cheatcodes/src/toml.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ pub(super) fn toml_to_json_value(toml: TomlValue) -> JsonValue {
234234
_ => JsonValue::String(s),
235235
},
236236
TomlValue::Integer(i) => JsonValue::Number(i.into()),
237-
TomlValue::Float(f) => JsonValue::Number(serde_json::Number::from_f64(f).unwrap()),
237+
TomlValue::Float(f) => match serde_json::Number::from_f64(f) {
238+
Some(n) => JsonValue::Number(n),
239+
None => JsonValue::String(f.to_string()),
240+
},
238241
TomlValue::Boolean(b) => JsonValue::Bool(b),
239242
TomlValue::Array(a) => JsonValue::Array(a.into_iter().map(toml_to_json_value).collect()),
240243
TomlValue::Table(t) => {

testdata/default/cheats/Toml.t.sol

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,24 @@ contract ParseTomlTest is DSTest {
329329

330330
assertEq(keccak256(abi.encode(members)), keccak256(abi.encode(data.members)));
331331
}
332+
333+
function test_floatNaN() public {
334+
bytes memory data = vm.parseToml(toml, ".nanFloat");
335+
string memory decodedData = abi.decode(data, (string));
336+
assertEq("NaN", decodedData);
337+
}
338+
339+
function test_floatInf() public {
340+
bytes memory data = vm.parseToml(toml, ".infFloat");
341+
string memory decodedData = abi.decode(data, (string));
342+
assertEq("inf", decodedData);
343+
}
344+
345+
function test_floatNegInf() public {
346+
bytes memory data = vm.parseToml(toml, ".neginfFloat");
347+
string memory decodedData = abi.decode(data, (string));
348+
assertEq("-inf", decodedData);
349+
}
332350
}
333351

334352
contract WriteTomlTest is DSTest {

testdata/fixtures/Toml/test.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
nanFloat = nan
2+
infFloat = +inf
3+
neginfFloat = -inf
4+
15
basicString = "hai"
26
nullString = "null"
37
multilineString = """
@@ -48,3 +52,5 @@ id = 1
4852

4953
[[advancedTomlPath]]
5054
id = 2
55+
56+

0 commit comments

Comments
 (0)