summaryrefslogtreecommitdiffstats
path: root/Python/mystrtoul.c
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2006-10-04 12:17:45 (GMT)
committerArmin Rigo <arigo@tunes.org>2006-10-04 12:17:45 (GMT)
commit7ccbca93a27e22f0b06316b0d9760fbf7b19cbda (patch)
tree63a92638c4b4faa7bcd2979eadeef5aa33a1abd9 /Python/mystrtoul.c
parent0d2f498a4cebb428a28fdc54b277ecede5ebc1c7 (diff)
downloadcpython-7ccbca93a27e22f0b06316b0d9760fbf7b19cbda.zip
cpython-7ccbca93a27e22f0b06316b0d9760fbf7b19cbda.tar.gz
cpython-7ccbca93a27e22f0b06316b0d9760fbf7b19cbda.tar.bz2
Forward-port of r52136,52138: a review of overflow-detecting code.
* unified the way intobject, longobject and mystrtoul handle values around -sys.maxint-1. * in general, trying to entierely avoid overflows in any computation involving signed ints or longs is extremely involved. Fixed a few simple cases where a compiler might be too clever (but that's all guesswork). * more overflow checks against bad data in marshal.c. * 2.5 specific: fixed a number of places that were still confusing int and Py_ssize_t. Some of them could potentially have caused "real-world" breakage. * list.pop(x): fixing overflow issues on x was messy. I just reverted to PyArg_ParseTuple("n"), which does the right thing. (An obscure test was trying to give a Decimal to list.pop()... doesn't make sense any more IMHO) * trying to write a few tests...
Diffstat (limited to 'Python/mystrtoul.c')
-rw-r--r--Python/mystrtoul.c12
1 files changed, 4 insertions, 8 deletions
diff --git a/Python/mystrtoul.c b/Python/mystrtoul.c
index 0dda4be..f007057 100644
--- a/Python/mystrtoul.c
+++ b/Python/mystrtoul.c
@@ -195,13 +195,10 @@ overflowed:
return (unsigned long)-1;
}
-/* Checking for overflow in PyOS_strtol is a PITA since C doesn't define
- * anything about what happens when a signed integer operation overflows,
- * and some compilers think they're doing you a favor by being "clever"
- * then. Python assumes a 2's-complement representation, so that the bit
- * pattern for the largest postive signed long is LONG_MAX, and for
- * the smallest negative signed long is LONG_MAX + 1.
+/* Checking for overflow in PyOS_strtol is a PITA; see comments
+ * about PY_ABS_LONG_MIN in longobject.c.
*/
+#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
long
PyOS_strtol(char *str, char **ptr, int base)
@@ -224,8 +221,7 @@ PyOS_strtol(char *str, char **ptr, int base)
if (sign == '-')
result = -result;
}
- else if (sign == '-' && uresult == (unsigned long)LONG_MAX + 1) {
- assert(LONG_MIN == -LONG_MAX-1);
+ else if (sign == '-' && uresult == PY_ABS_LONG_MIN) {
result = LONG_MIN;
}
else {