https://github.com/python/cpython/commit/95f94d04d8772f926b6a9bc1e38b67218a1dba7a
commit: 95f94d04d8772f926b6a9bc1e38b67218a1dba7a
branch: main
author: Jelle Zijlstra <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-20T10:20:20-07:00
summary:

gh-159739: Reject the minimum dynamic formatting width (#159780)



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 a69a69537d568d..eff0e95c3af7f4 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -324,6 +324,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 a5c369757d452f..e9264f765a08e6 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -852,6 +852,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 caadf18f6a5d30..b090a68d2bda9f 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