diff Objects/unicodeobject.c @ 76817:064c2d0483f8

Issue #14700: Fix two broken and undefined-behaviour-inducing overflow checks in old-style string formatting. Thanks Serhiy Storchaka for report and original patch.
author Mark Dickinson <mdickinson@enthought.com>
date Mon, 07 May 2012 11:20:50 +0100
parents 44699acd28b4
children 7be716a47e9d
line wrap: on
line diff
--- a/Objects/unicodeobject.c	Mon May 07 02:44:50 2012 -0700
+++ b/Objects/unicodeobject.c	Mon May 07 11:20:50 2012 +0100
@@ -13933,7 +13933,7 @@
                     c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
                     if (c < '0' || c > '9')
                         break;
-                    if ((width*10) / 10 != width) {
+                    if (width > (PY_SSIZE_T_MAX - (c - '0')) / 10) {
                         PyErr_SetString(PyExc_ValueError,
                                         "width too big");
                         goto onError;
@@ -13968,7 +13968,7 @@
                         c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
                         if (c < '0' || c > '9')
                             break;
-                        if ((prec*10) / 10 != prec) {
+                        if (prec > (INT_MAX - (c - '0')) / 10) {
                             PyErr_SetString(PyExc_ValueError,
                                             "prec too big");
                             goto onError;