diff options
author | Mark Dickinson <mdickinson@enthought.com> | 2011-09-25 14:34:32 (GMT) |
---|---|---|
committer | Mark Dickinson <mdickinson@enthought.com> | 2011-09-25 14:34:32 (GMT) |
commit | c7d93b761413834e0ac39f5fc648565a2843121f (patch) | |
tree | 0bcb66dfcb476fb49a59ce019943f293f003dbc0 /Objects/stringlib | |
parent | 50203a69b344e80be5000fe87aafad09e84cde85 (diff) | |
download | cpython-c7d93b761413834e0ac39f5fc648565a2843121f.zip cpython-c7d93b761413834e0ac39f5fc648565a2843121f.tar.gz cpython-c7d93b761413834e0ac39f5fc648565a2843121f.tar.bz2 |
Issue #1621: Fix undefined behaviour from signed overflow in datetime module hashes, array and list iterations, and get_integer (stringlib/string_format.h)
Diffstat (limited to 'Objects/stringlib')
-rw-r--r-- | Objects/stringlib/string_format.h | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/Objects/stringlib/string_format.h b/Objects/stringlib/string_format.h index 6c7adcb..d992b6f 100644 --- a/Objects/stringlib/string_format.h +++ b/Objects/stringlib/string_format.h @@ -209,19 +209,17 @@ get_integer(const SubString *str) if (digitval < 0) return -1; /* - This trick was copied from old Unicode format code. It's cute, - but would really suck on an old machine with a slow divide - implementation. Fortunately, in the normal case we do not - expect too many digits. + Detect possible overflow before it happens: + + accumulator * 10 + digitval > PY_SSIZE_T_MAX if and only if + accumulator > (PY_SSIZE_T_MAX - digitval) / 10. */ - oldaccumulator = accumulator; - accumulator *= 10; - if ((accumulator+10)/10 != oldaccumulator+1) { + if (accumulator > (PY_SSIZE_T_MAX - digitval) / 10) { PyErr_Format(PyExc_ValueError, "Too many decimal digits in format string"); return -1; } - accumulator += digitval; + accumulator = accumulator * 10 + digitval; } return accumulator; } |