https://github.com/python/cpython/commit/ce9715b283eb4a165381f8a7ff0c6684a201210b
commit: ce9715b283eb4a165381f8a7ff0c6684a201210b
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-20T14:57:25-07:00
summary:

[3.15] gh-159739: Reject the minimum dynamic formatting width (GH-159780) 
(#159906)



(cherry picked from commit 95f94d04d8772f926b6a9bc1e38b67218a1dba7a)

Co-authored-by: Jelle Zijlstra <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.78e530.rst
M Lib/test/test_format.py
M Objects/bytesobject.c
M Objects/unicode_format.c

diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index 4f253e01c0b174..618c38f34626f7 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -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,
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.78e530.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.78e530.rst
new file mode 100644
index 00000000000000..999efdac87f974
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.78e530.rst
@@ -0,0 +1,2 @@
+Raise :exc:`OverflowError` when the absolute value of a dynamic printf-style
+formatting width is too large.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 350d3434ebcbd8..60fc73f044daba 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -792,6 +792,10 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t 
format_len,
                     goto error;
                 }
                 if (width < 0) {
+                    if (width == PY_SSIZE_T_MIN) {
+                        FORMAT_ERROR(PyExc_OverflowError, "too big for 
width%s", "");
+                        goto error;
+                    }
                     flags |= F_LJUST;
                     width = -width;
                 }
diff --git a/Objects/unicode_format.c b/Objects/unicode_format.c
index 1d6f3f7d9a6f6a..67b189d8c6356b 100644
--- a/Objects/unicode_format.c
+++ b/Objects/unicode_format.c
@@ -564,6 +564,10 @@ unicode_format_arg_parse(struct unicode_formatter_t *ctx,
             return -1;
         }
         if (arg->width < 0) {
+            if (arg->width == PY_SSIZE_T_MIN) {
+                FORMAT_ERROR(PyExc_OverflowError, "too big for width%s", "");
+                return -1;
+            }
             arg->flags |= F_LJUST;
             arg->width = -arg->width;
         }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to