Skip to content

Commit ce9715b

Browse files
miss-islingtonjelle-openaiJelleZijlstra
authored
[3.15] gh-159739: Reject the minimum dynamic formatting width (GH-159780) (#159906)
(cherry picked from commit 95f94d0) Co-authored-by: Jelle Zijlstra <jelle@openai.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 6a23dc7 commit ce9715b

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
@@ -318,6 +318,8 @@ def test_common_format(self):
318318
"format argument 1: too big for width")
319319
test_exc_common('%*r', (-2**1000, 1), OverflowError,
320320
"format argument 1: too big for width")
321+
test_exc_common('%*r', (-maxsize - 1, 1), OverflowError,
322+
"format argument 1: too big for width")
321323
test_exc_common('%.*r', (2**1000, 1), OverflowError,
322324
"format argument 1: too big for precision")
323325
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
@@ -792,6 +792,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
792792
goto error;
793793
}
794794
if (width < 0) {
795+
if (width == PY_SSIZE_T_MIN) {
796+
FORMAT_ERROR(PyExc_OverflowError, "too big for width%s", "");
797+
goto error;
798+
}
795799
flags |= F_LJUST;
796800
width = -width;
797801
}

‎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)