| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0a54cf1 commit 47862d4
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,7 +51,7 @@ static int | |||
| 51 | 51 | get_integer(PyObject *str, Py_ssize_t *pos, Py_ssize_t end, | |
| 52 | 52 | Py_ssize_t *result) | |
| 53 | 53 | { | |
| 54 | - Py_ssize_t accumulator, digitval, oldaccumulator; | ||
| 54 | + Py_ssize_t accumulator, digitval; | ||
| 55 | 55 | int numdigits; | |
| 56 | 56 | accumulator = numdigits = 0; | |
| 57 | 57 | for (;;(*pos)++, numdigits++) { | |
@@ -61,19 +61,17 @@ get_integer(PyObject *str, Py_ssize_t *pos, Py_ssize_t end, | |||
| 61 | 61 | if (digitval < 0) | |
| 62 | 62 | break; | |
| 63 | 63 | /* | |
| 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. | ||
| 68 | 68 | */ | |
| 69 | - oldaccumulator = accumulator; | ||
| 70 | - accumulator *= 10; | ||
| 71 | - if ((accumulator+10)/10 != oldaccumulator+1) { | ||
| 69 | + if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) { | ||
| 72 | 70 | PyErr_Format(PyExc_ValueError, | |
| 73 | 71 | "Too many decimal digits in format string"); | |
| 74 | 72 | return -1; | |
| 75 | 73 | } | |
| 76 | - accumulator += digitval; | ||
| 74 | + accumulator = accumulator * 10 + digitval; | ||
| 77 | 75 | } | |
| 78 | 76 | *result = accumulator; | |
| 79 | 77 | return numdigits; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments