Skip to content
Open
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 @@ -318,6 +318,8 @@ def test_common_format(self):
"format argument 1: too big for width")
test_exc_common('%*r', (-2**1000, 1), OverflowError,
"format argument 1: too big for width")
test_exc_common('%*r', (-maxsize - 1, 1), OverflowError,
"format argument 1: too big for width")
test_exc_common('%.*r', (2**1000, 1), OverflowError,
"format argument 1: too big for precision")
test_exc_common('%.*r', (-2**1000, 1), OverflowError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
When formatting a ``str`` or ``bytes`` with ``%``, passing ``-sys.maxsize -
1`` as the width previously negated it, this is undefined behaviour in C. It
now raises :exc:`OverflowError`.
5 changes: 5 additions & 0 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,11 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
}
if (width < 0) {
flags |= F_LJUST;
if (width < -PY_SSIZE_T_MAX) {
FORMAT_ERROR(PyExc_OverflowError,
"too big for width%s", "");
goto error;
}
width = -width;
}
if (--fmtcnt >= 0)
Expand Down
4 changes: 4 additions & 0 deletions Objects/unicode_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,10 @@ unicode_format_arg_parse(struct unicode_formatter_t *ctx,
}
if (arg->width < 0) {
arg->flags |= F_LJUST;
if (arg->width < -PY_SSIZE_T_MAX) {
FORMAT_ERROR(PyExc_OverflowError, "too big for width%s", "");
return -1;
}
arg->width = -arg->width;
}
if (--ctx->fmtcnt >= 0) {
Expand Down
Loading