Skip to content

Commit 20c33c1

Browse files
authored
[3.14] gh-159739: Reject the minimum dynamic formatting width (GH-159780) (#159931)
Adapt the str formatting fix to Objects/unicodeobject.c and use the error wording already present in the 3.14 formatters. (cherry picked from commit 95f94d0)
1 parent d920ff9 commit 20c33c1

4 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎Lib/test/test_format.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ def test_common_format(self):
287287
"%i format: a real number is required, not str")
288288
test_exc_common('%i', b'1', TypeError,
289289
"%i format: a real number is required, not bytes")
290+
test_exc_common('%*r', (-maxsize - 1, 1), OverflowError,
291+
"width too big")
290292

291293
def test_str_format(self):
292294
testformat("%r", "\u0378", "'\\u0378'") # non printable
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Raise :exc:`OverflowError` when the absolute value of a dynamic printf-style
2+
formatting width is too large.

‎Objects/bytesobject.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
752752
if (width == -1 && PyErr_Occurred())
753753
goto error;
754754
if (width < 0) {
755+
if (width == PY_SSIZE_T_MIN) {
756+
PyErr_SetString(PyExc_OverflowError, "width too big");
757+
goto error;
758+
}
755759
flags |= F_LJUST;
756760
width = -width;
757761
}

‎Objects/unicodeobject.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15150,6 +15150,10 @@ unicode_format_arg_parse(struct unicode_formatter_t *ctx,
1515015150
if (arg->width == -1 && PyErr_Occurred())
1515115151
return -1;
1515215152
if (arg->width < 0) {
15153+
if (arg->width == PY_SSIZE_T_MIN) {
15154+
PyErr_SetString(PyExc_OverflowError, "width too big");
15155+
return -1;
15156+
}
1515315157
arg->flags |= F_LJUST;
1515415158
arg->width = -arg->width;
1515515159
}

0 commit comments

Comments
 (0)