summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2012-10-06 17:05:14 (GMT)
committerMark Dickinson <mdickinson@enthought.com>2012-10-06 17:05:14 (GMT)
commitff9c54aca28592a3eab0bdba1ddd977b441e1e9f (patch)
tree9ffb1c0c0b9aad8615cfae28c3ebbea5c67c4752 /Objects
parent7e3cde590140052776ad219817e7bd7b87e3d4cd (diff)
parentc04ddff290fc203d05b75c8569b748525fb76b5b (diff)
downloadcpython-ff9c54aca28592a3eab0bdba1ddd977b441e1e9f.zip
cpython-ff9c54aca28592a3eab0bdba1ddd977b441e1e9f.tar.gz
cpython-ff9c54aca28592a3eab0bdba1ddd977b441e1e9f.tar.bz2
Issue #16096: Merge fixes from 3.3.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/tupleobject.c12
-rw-r--r--Objects/unicodeobject.c23
2 files changed, 13 insertions, 22 deletions
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index b76125a..9c843fa 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -96,15 +96,11 @@ PyTuple_New(register Py_ssize_t size)
else
#endif
{
- Py_ssize_t nbytes = size * sizeof(PyObject *);
/* Check for overflow */
- if (nbytes / sizeof(PyObject *) != (size_t)size ||
- (nbytes > PY_SSIZE_T_MAX - sizeof(PyTupleObject) - sizeof(PyObject *)))
- {
+ if (size > (PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
+ sizeof(PyObject *)) / sizeof(PyObject *)) {
return PyErr_NoMemory();
}
- /* nbytes += sizeof(PyTupleObject) - sizeof(PyObject *); */
-
op = PyObject_GC_NewVar(PyTupleObject, &PyTuple_Type, size);
if (op == NULL)
return NULL;
@@ -481,9 +477,9 @@ tuplerepeat(PyTupleObject *a, Py_ssize_t n)
if (Py_SIZE(a) == 0)
return PyTuple_New(0);
}
- size = Py_SIZE(a) * n;
- if (size/Py_SIZE(a) != n)
+ if (n > PY_SSIZE_T_MAX / Py_SIZE(a))
return PyErr_NoMemory();
+ size = Py_SIZE(a) * n;
np = (PyTupleObject *) PyTuple_New(size);
if (np == NULL)
return NULL;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 84bbf9a..c3bf392 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -4528,7 +4528,6 @@ _PyUnicode_EncodeUTF7(PyObject *str,
void *data;
Py_ssize_t len;
PyObject *v;
- Py_ssize_t allocated;
int inShift = 0;
Py_ssize_t i;
unsigned int base64bits = 0;
@@ -4546,11 +4545,9 @@ _PyUnicode_EncodeUTF7(PyObject *str,
return PyBytes_FromStringAndSize(NULL, 0);
/* It might be possible to tighten this worst case */
- allocated = 8 * len;
- if (allocated / 8 != len)
+ if (len > PY_SSIZE_T_MAX / 8)
return PyErr_NoMemory();
-
- v = PyBytes_FromStringAndSize(NULL, allocated);
+ v = PyBytes_FromStringAndSize(NULL, len * 8);
if (v == NULL)
return NULL;
@@ -5128,7 +5125,7 @@ _PyUnicode_EncodeUTF32(PyObject *str,
Py_ssize_t len;
PyObject *v;
unsigned char *p;
- Py_ssize_t nsize, bytesize, i;
+ Py_ssize_t nsize, i;
/* Offsets from p for storing byte pairs in the right order. */
#ifdef BYTEORDER_IS_LITTLE_ENDIAN
int iorder[] = {0, 1, 2, 3};
@@ -5156,10 +5153,9 @@ _PyUnicode_EncodeUTF32(PyObject *str,
len = PyUnicode_GET_LENGTH(str);
nsize = len + (byteorder == 0);
- bytesize = nsize * 4;
- if (bytesize / 4 != nsize)
+ if (nsize > PY_SSIZE_T_MAX / 4)
return PyErr_NoMemory();
- v = PyBytes_FromStringAndSize(NULL, bytesize);
+ v = PyBytes_FromStringAndSize(NULL, nsize * 4);
if (v == NULL)
return NULL;
@@ -10195,7 +10191,7 @@ replace(PyObject *self, PyObject *str1,
}
else {
Py_ssize_t n, i, j, ires;
- Py_ssize_t product, new_size;
+ Py_ssize_t new_size;
int rkind = skind;
char *res;
@@ -10227,19 +10223,18 @@ replace(PyObject *self, PyObject *str1,
}
/* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) -
PyUnicode_GET_LENGTH(str1))); */
- product = n * (len2-len1);
- if ((product / (len2-len1)) != n) {
+ if (len2 > len1 && len2 - len1 > (PY_SSIZE_T_MAX - slen) / n) {
PyErr_SetString(PyExc_OverflowError,
"replace string is too long");
goto error;
}
- new_size = slen + product;
+ new_size = slen + n * (len2 - len1);
if (new_size == 0) {
Py_INCREF(unicode_empty);
u = unicode_empty;
goto done;
}
- if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) {
+ if (new_size > (PY_SSIZE_T_MAX >> (rkind-1))) {
PyErr_SetString(PyExc_OverflowError,
"replace string is too long");
goto error;