FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Issue #9530: Fix undefined behaviour due to signed overflow in Python… · python-emscripten/cpython@47862d4 · GitHub

Repository navigation

Commit 47862d4

Browse files
committed
Issue python#9530: Fix undefined behaviour due to signed overflow in Python/formatter_unicode.c.
1 parent 0a54cf1 commit 47862d4

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

‎Python/formatter_unicode.c‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static int
5151
get_integer(PyObject *str, Py_ssize_t *pos, Py_ssize_t end,
5252
Py_ssize_t *result)
5353
{
54-
Py_ssize_t accumulator, digitval, oldaccumulator;
54+
Py_ssize_t accumulator, digitval;
5555
int numdigits;
5656
accumulator = numdigits = 0;
5757
for (;;(*pos)++, numdigits++) {
@@ -61,19 +61,17 @@ get_integer(PyObject *str, Py_ssize_t *pos, Py_ssize_t end,
6161
if (digitval < 0)
6262
break;
6363
/*
64-
This trick was copied from old Unicode format code. It's cute,
65-
but would really suck on an old machine with a slow divide
66-
implementation. Fortunately, in the normal case we do not
67-
expect too many digits.
64+
Detect possible overflow before it happens:
65+
66+
accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if
67+
accumulator > (PY_SSIZE_T_MAX - digitval) / 10.
6868
*/
69-
oldaccumulator = accumulator;
70-
accumulator *= 10;
71-
if ((accumulator+10)/10 != oldaccumulator+1) {
69+
if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) {
7270
PyErr_Format(PyExc_ValueError,
7371
"Too many decimal digits in format string");
7472
return -1;
7573
}
76-
accumulator += digitval;
74+
accumulator = accumulator * 10 + digitval;
7775
}
7876
*result = accumulator;
7977
return numdigits;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL