mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
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...
This commit is contained in:
parent
0d2f498a4c
commit
7ccbca93a2
19 changed files with 186 additions and 106 deletions
|
@ -98,7 +98,7 @@ type_module(PyTypeObject *type, void *context)
|
|||
s = strrchr(type->tp_name, '.');
|
||||
if (s != NULL)
|
||||
return PyString_FromStringAndSize(
|
||||
type->tp_name, (int)(s - type->tp_name));
|
||||
type->tp_name, (Py_ssize_t)(s - type->tp_name));
|
||||
return PyString_FromString("__builtin__");
|
||||
}
|
||||
}
|
||||
|
@ -4116,19 +4116,10 @@ slot_sq_length(PyObject *self)
|
|||
return -1;
|
||||
len = PyInt_AsSsize_t(res);
|
||||
Py_DECREF(res);
|
||||
if (len == -1 && PyErr_Occurred())
|
||||
return -1;
|
||||
#if SIZEOF_SIZE_T < SIZEOF_INT
|
||||
/* Overflow check -- range of PyInt is more than C ssize_t */
|
||||
if (len != (int)len) {
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"__len__() should return 0 <= outcome < 2**31");
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
if (len < 0) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"__len__() should return >= 0");
|
||||
if (!PyErr_Occurred())
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"__len__() should return >= 0");
|
||||
return -1;
|
||||
}
|
||||
return len;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue