Skip to content

Commit 5b556a0

Browse files
SiyetАлександр Сергеевич Целуйко
andauthored
Correct error messages for out-of-range Meta length bounds (msgspec#1172)
Calling `Meta(min_length=10**400)` results in a `ValueError` with the incorrect message "must be >= 0" for a positive integer. With this fix, out-of-range `min_length` and `max_length` values result in an error identifying the range issue. Accepted values and diagnostics for representable negative values are unchanged. This is the length-bound fix discussed in msgspec#1122, separate from the numeric constraints work in msgspec#1006. Co-authored-by: Александр Сергеевич Целуйко <[email protected]>
1 parent 2e2c863 commit 5b556a0

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎src/msgspec/_core.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,17 @@ ensure_is_nonnegative_integer(PyObject *val, const char *param) {
17261726
}
17271727
Py_ssize_t x = PyLong_AsSsize_t(val);
17281728
if (x >= 0) return true;
1729+
if (PyErr_Occurred()) {
1730+
/* Doesn't fit in a Py_ssize_t, in either direction. Without this the
1731+
* pending OverflowError is replaced by the message below, which for a
1732+
* large positive value says it isn't >= 0. */
1733+
PyErr_Clear();
1734+
PyErr_Format(
1735+
PyExc_ValueError, "`%s` is out of range, %R is not a valid length",
1736+
param, val
1737+
);
1738+
return false;
1739+
}
17291740
PyErr_Format(PyExc_ValueError, "`%s` must be >= 0, got %R", param, val);
17301741
return false;
17311742
}

‎tests/unit/test_constraints.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import math
33
import re
4+
import sys
45
from typing import Annotated
56

67
import pytest
@@ -168,10 +169,27 @@ def test_multiple_of_bounds(self, val):
168169
def test_nonnegative_integer_fields(self, field):
169170
Meta(**{field: 0})
170171
Meta(**{field: 10})
172+
Meta(**{field: sys.maxsize})
171173
with pytest.raises(TypeError, match=f"`{field}` must be an int, got float"):
172174
Meta(**{field: 1.5})
173-
with pytest.raises(ValueError, match=f"{field}` must be >= 0, got -10"):
175+
176+
with pytest.raises(ValueError, match=f"`{field}` must be >= 0, got -1"):
177+
Meta(**{field: -1})
178+
with pytest.raises(ValueError, match=f"`{field}` must be >= 0, got -10"):
174179
Meta(**{field: -10})
180+
with pytest.raises(
181+
ValueError, match=f"`{field}` must be >= 0, got {-sys.maxsize - 1}"
182+
):
183+
Meta(**{field: -sys.maxsize - 1})
184+
185+
@pytest.mark.parametrize("field", ["min_length", "max_length"])
186+
@pytest.mark.parametrize(
187+
"val", [sys.maxsize + 1, -sys.maxsize - 2, 10**400, -(10**400)]
188+
)
189+
def test_nonnegative_integer_fields_out_of_range(self, field, val):
190+
# Values outside Py_ssize_t need a range error in either direction.
191+
with pytest.raises(ValueError, match=f"`{field}` is out of range"):
192+
Meta(**{field: val})
175193

176194
@pytest.mark.parametrize("field", ["pattern", "title", "description"])
177195
def test_string_fields(self, field):

0 commit comments

Comments
 (0)