summaryrefslogtreecommitdiffstats
path: root/Objects/tupleobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1999-07-12 23:06:58 (GMT)
committerGuido van Rossum <guido@python.org>1999-07-12 23:06:58 (GMT)
commit5bc51f2f27dfeb57ae08b659ef1aa0c035077d60 (patch)
tree1752954a9f59913e4110ce5e03e511106d2cd1a3 /Objects/tupleobject.c
parent7c5b9d1fa95df002801b0ae810d2b58e33a34db6 (diff)
downloadcpython-5bc51f2f27dfeb57ae08b659ef1aa0c035077d60.zip
cpython-5bc51f2f27dfeb57ae08b659ef1aa0c035077d60.tar.gz
cpython-5bc51f2f27dfeb57ae08b659ef1aa0c035077d60.tar.bz2
Appropriate overflow checks so that things like sys.maxint*(1,) can't
dump core.
Diffstat (limited to 'Objects/tupleobject.c')
-rw-r--r--Objects/tupleobject.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 4b7714c..225835c 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -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;