Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/test/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ def test_common_format(self):
"%i format: a real number is required, not str")
test_exc_common('%i', b'1', TypeError,
"%i format: a real number is required, not bytes")
test_exc_common('%*r', (-maxsize - 1, 1), OverflowError,
"width too big")

def test_str_format(self):
testformat("%r", "\u0378", "'\\u0378'") # non printable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`OverflowError` when the absolute value of a dynamic printf-style
formatting width is too large.
4 changes: 4 additions & 0 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
if (width == -1 && PyErr_Occurred())
goto error;
if (width < 0) {
if (width == PY_SSIZE_T_MIN) {
PyErr_SetString(PyExc_OverflowError, "width too big");
goto error;
}
flags |= F_LJUST;
width = -width;
}
Expand Down
4 changes: 4 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -15150,6 +15150,10 @@ unicode_format_arg_parse(struct unicode_formatter_t *ctx,
if (arg->width == -1 && PyErr_Occurred())
return -1;
if (arg->width < 0) {
if (arg->width == PY_SSIZE_T_MIN) {
PyErr_SetString(PyExc_OverflowError, "width too big");
return -1;
}
arg->flags |= F_LJUST;
arg->width = -arg->width;
}
Expand Down
Loading