Skip to content

Commit 5be57b7

Browse files
committed
Fix SystemError when convert()'ing an out-of-range int to float
msgspec.convert(obj, float) converts a python int to a C double via PyLong_AsDouble without checking for overflow. For an int too large to represent as a float, PyLong_AsDouble sets OverflowError but still returns -1.0, and that value was passed straight through to ms_decode_float and returned to the interpreter with the exception still set - which surfaces as SystemError: <built-in function convert> returned a result with an exception set, bypassing the documented ValidationError contract entirely. json.decode already handles the equivalent case correctly, raising ValidationError: Number out of range. Apply the same value-error-with-path check convert() already uses elsewhere in this file (e.g. _constr_as_f64) so the overflow is reported the same way, for both bare and nested (e.g. dict[str, float]) targets, under both strict and lax mode. Fixes #1122.
1 parent f51f378 commit 5be57b7

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎src/msgspec/_core.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20937,7 +20937,14 @@ convert_int(
2093720937
return ms_decode_int_enum_or_literal_pyint(obj, type, path);
2093820938
}
2093920939
else if (type->types & MS_TYPE_FLOAT) {
20940-
return ms_decode_float(PyLong_AsDouble(obj), type, path);
20940+
double val = PyLong_AsDouble(obj);
20941+
if (val == -1.0 && PyErr_Occurred()) {
20942+
/* `obj` is out of range for a C double (PyLong_AsDouble sets
20943+
* OverflowError but still returns -1.0); without this check
20944+
* that error leaks past this function as a SystemError. */
20945+
return ms_error_with_path("Number out of range%U", path);
20946+
}
20947+
return ms_decode_float(val, type, path);
2094120948
}
2094220949
else if (
2094320950
type->types & MS_TYPE_DECIMAL

‎tests/unit/test_convert.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,17 @@ def test_float(self):
429429
with pytest.raises(ValidationError, match="Expected `float`, got `null`"):
430430
convert(None, float)
431431

432+
@pytest.mark.parametrize("strict", [True, False])
433+
def test_float_from_int_out_of_range(self, strict):
434+
# A python int too large to represent as a C double must raise a
435+
# ValidationError, matching json.decode, rather than leaking the
436+
# OverflowError PyLong_AsDouble sets as a SystemError.
437+
big = 10**400
438+
with pytest.raises(ValidationError, match="Number out of range"):
439+
convert(big, float, strict=strict)
440+
with pytest.raises(ValidationError, match="Number out of range"):
441+
convert({"x": big}, dict[str, float], strict=strict)
442+
432443
@pytest.mark.parametrize(
433444
"meta, good, bad",
434445
[

0 commit comments

Comments
 (0)