Skip to content

Commit 5fb47c1

Browse files
committed
gh-159739: Reject the minimum dynamic formatting width
Backport to 3.15 and 3.14 (adapt the str formatting fix to Objects/unicodeobject.c on 3.14).
1 parent 597ae8f commit 5fb47c1

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
@@ -324,6 +324,8 @@ def test_common_format(self):
324324
"format argument 1: too big for width")
325325
test_exc_common('%*r', (-2**1000, 1), OverflowError,
326326
"format argument 1: too big for width")
327+
test_exc_common('%*r', (-maxsize - 1, 1), OverflowError,
328+
"format argument 1: too big for width")
327329
test_exc_common('%.*r', (2**1000, 1), OverflowError,
328330
"format argument 1: too big for precision")
329331
test_exc_common('%.*r', (-2**1000, 1), OverflowError,
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
@@ -852,6 +852,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
852852
goto error;
853853
}
854854
if (width < 0) {
855+
if (width == PY_SSIZE_T_MIN) {
856+
FORMAT_ERROR(PyExc_OverflowError, "too big for width%s", "");
857+
goto error;
858+
}
855859
flags |= F_LJUST;
856860
width = -width;
857861
}

‎Objects/unicode_format.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ unicode_format_arg_parse(struct unicode_formatter_t *ctx,
564564
return -1;
565565
}
566566
if (arg->width < 0) {
567+
if (arg->width == PY_SSIZE_T_MIN) {
568+
FORMAT_ERROR(PyExc_OverflowError, "too big for width%s", "");
569+
return -1;
570+
}
567571
arg->flags |= F_LJUST;
568572
arg->width = -arg->width;
569573
}

0 commit comments

Comments
 (0)