Appropriate overflow checks so that things like sys.maxint*(1,) can't

dump core.
This commit is contained in:
Guido van Rossum 1999-07-12 23:06:58 +00:00
parent 7c5b9d1fa9
commit 5bc51f2f27
1 changed files with 13 additions and 3 deletions

View File

@ -82,8 +82,16 @@ PyTuple_New(size)
else
#endif
{
op = (PyTupleObject *) malloc(
sizeof(PyTupleObject) + (size-1) * sizeof(PyObject *));
int nbytes = size * sizeof(PyObject *);
/* Check for overflow */
if (nbytes / sizeof(PyObject *) != (size_t)size ||
(nbytes += sizeof(PyTupleObject) - sizeof(PyObject *))
<= 0)
{
return PyErr_NoMemory();
}
;
op = (PyTupleObject *) malloc(nbytes);
if (op == NULL)
return PyErr_NoMemory();
@ -359,13 +367,15 @@ tuplerepeat(a, n)
PyObject **p;
if (n < 0)
n = 0;
if (a->ob_size*n == a->ob_size) {
if (a->ob_size == 0 || n == 1) {
/* Since tuples are immutable, we can return a shared
copy in this case */
Py_INCREF(a);
return (PyObject *)a;
}
size = a->ob_size * n;
if (size/n != a->ob_size)
return PyErr_NoMemory();
np = (PyTupleObject *) PyTuple_New(size);
if (np == NULL)
return NULL;