Commit 6c7a52a4 authored by Victor Stinner's avatar Victor Stinner

Check for PyUnicode_CopyCharacters() failure

parent be78eaf2
...@@ -1784,9 +1784,10 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -1784,9 +1784,10 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
(void) va_arg(vargs, char *); (void) va_arg(vargs, char *);
size = PyUnicode_GET_LENGTH(*callresult); size = PyUnicode_GET_LENGTH(*callresult);
assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
PyUnicode_CopyCharacters((PyObject*)string, i, if (PyUnicode_CopyCharacters((PyObject*)string, i,
*callresult, 0, *callresult, 0,
size); size) < 0)
goto fail;
i += size; i += size;
/* We're done with the unicode()/repr() => forget it */ /* We're done with the unicode()/repr() => forget it */
Py_DECREF(*callresult); Py_DECREF(*callresult);
...@@ -1800,9 +1801,10 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -1800,9 +1801,10 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
Py_ssize_t size; Py_ssize_t size;
assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
size = PyUnicode_GET_LENGTH(obj); size = PyUnicode_GET_LENGTH(obj);
PyUnicode_CopyCharacters((PyObject*)string, i, if (PyUnicode_CopyCharacters((PyObject*)string, i,
obj, 0, obj, 0,
size); size) < 0)
goto fail;
i += size; i += size;
break; break;
} }
...@@ -1814,17 +1816,19 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -1814,17 +1816,19 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
if (obj) { if (obj) {
size = PyUnicode_GET_LENGTH(obj); size = PyUnicode_GET_LENGTH(obj);
assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string)); assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
PyUnicode_CopyCharacters((PyObject*)string, i, if (PyUnicode_CopyCharacters((PyObject*)string, i,
obj, 0, obj, 0,
size); size) < 0)
goto fail;
i += size; i += size;
} else { } else {
size = PyUnicode_GET_LENGTH(*callresult); size = PyUnicode_GET_LENGTH(*callresult);
assert(PyUnicode_KIND(*callresult) <= assert(PyUnicode_KIND(*callresult) <=
PyUnicode_KIND(string)); PyUnicode_KIND(string));
PyUnicode_CopyCharacters((PyObject*)string, i, if (PyUnicode_CopyCharacters((PyObject*)string, i,
*callresult, *callresult,
0, size); 0, size) < 0)
goto fail;
i += size; i += size;
Py_DECREF(*callresult); Py_DECREF(*callresult);
} }
...@@ -1838,9 +1842,10 @@ PyUnicode_FromFormatV(const char *format, va_list vargs) ...@@ -1838,9 +1842,10 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
/* unused, since we already have the result */ /* unused, since we already have the result */
(void) va_arg(vargs, PyObject *); (void) va_arg(vargs, PyObject *);
assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string)); assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
PyUnicode_CopyCharacters((PyObject*)string, i, if (PyUnicode_CopyCharacters((PyObject*)string, i,
*callresult, 0, *callresult, 0,
PyUnicode_GET_LENGTH(*callresult)); PyUnicode_GET_LENGTH(*callresult)) < 0)
goto fail;
i += PyUnicode_GET_LENGTH(*callresult); i += PyUnicode_GET_LENGTH(*callresult);
/* We're done with the unicode()/repr() => forget it */ /* We're done with the unicode()/repr() => forget it */
Py_DECREF(*callresult); Py_DECREF(*callresult);
...@@ -8141,8 +8146,7 @@ fixup(PyUnicodeObject *self, ...@@ -8141,8 +8146,7 @@ fixup(PyUnicodeObject *self,
else { else {
/* In case the maximum character changed, we need to /* In case the maximum character changed, we need to
convert the string to the new category. */ convert the string to the new category. */
PyObject *v = PyUnicode_New( PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new);
PyUnicode_GET_LENGTH(self), maxchar_new);
if (v == NULL) { if (v == NULL) {
Py_DECREF(u); Py_DECREF(u);
return NULL; return NULL;
...@@ -8151,12 +8155,25 @@ fixup(PyUnicodeObject *self, ...@@ -8151,12 +8155,25 @@ fixup(PyUnicodeObject *self,
/* If the maxchar increased so that the kind changed, not all /* If the maxchar increased so that the kind changed, not all
characters are representable anymore and we need to fix the characters are representable anymore and we need to fix the
string again. This only happens in very few cases. */ string again. This only happens in very few cases. */
PyUnicode_CopyCharacters(v, 0, (PyObject*)self, 0, PyUnicode_GET_LENGTH(self)); if (PyUnicode_CopyCharacters(v, 0,
(PyObject*)self, 0,
PyUnicode_GET_LENGTH(self)) < 0)
{
Py_DECREF(u);
return NULL;
}
maxchar_old = fixfct((PyUnicodeObject*)v); maxchar_old = fixfct((PyUnicodeObject*)v);
assert(maxchar_old > 0 && maxchar_old <= maxchar_new); assert(maxchar_old > 0 && maxchar_old <= maxchar_new);
} }
else else {
PyUnicode_CopyCharacters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); if (PyUnicode_CopyCharacters(v, 0,
u, 0,
PyUnicode_GET_LENGTH(self)) < 0)
{
Py_DECREF(u);
return NULL;
}
}
Py_DECREF(u); Py_DECREF(u);
return v; return v;
...@@ -8455,12 +8472,14 @@ PyUnicode_Join(PyObject *separator, PyObject *seq) ...@@ -8455,12 +8472,14 @@ PyUnicode_Join(PyObject *separator, PyObject *seq)
itemlen = PyUnicode_GET_LENGTH(item); itemlen = PyUnicode_GET_LENGTH(item);
/* Copy item, and maybe the separator. */ /* Copy item, and maybe the separator. */
if (i) { if (i) {
PyUnicode_CopyCharacters(res, res_offset, if (PyUnicode_CopyCharacters(res, res_offset,
sep, 0, seplen); sep, 0, seplen) < 0)
goto onError;
res_offset += seplen; res_offset += seplen;
} }
PyUnicode_CopyCharacters(res, res_offset, if (PyUnicode_CopyCharacters(res, res_offset,
item, 0, itemlen); item, 0, itemlen) < 0)
goto onError;
res_offset += itemlen; res_offset += itemlen;
} }
assert(res_offset == PyUnicode_GET_LENGTH(res)); assert(res_offset == PyUnicode_GET_LENGTH(res));
...@@ -8508,6 +8527,8 @@ pad(PyUnicodeObject *self, ...@@ -8508,6 +8527,8 @@ pad(PyUnicodeObject *self,
{ {
PyObject *u; PyObject *u;
Py_UCS4 maxchar; Py_UCS4 maxchar;
int kind;
void *data;
if (left < 0) if (left < 0)
left = 0; left = 0;
...@@ -8528,14 +8549,21 @@ pad(PyUnicodeObject *self, ...@@ -8528,14 +8549,21 @@ pad(PyUnicodeObject *self,
if (fill > maxchar) if (fill > maxchar)
maxchar = fill; maxchar = fill;
u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar); u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar);
if (u) { if (!u)
int kind = PyUnicode_KIND(u); return NULL;
void *data = PyUnicode_DATA(u);
if (left) kind = PyUnicode_KIND(u);
FILL(kind, data, fill, 0, left); data = PyUnicode_DATA(u);
if (right) if (left)
FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right); FILL(kind, data, fill, 0, left);
PyUnicode_CopyCharacters(u, left, (PyObject*)self, 0, _PyUnicode_LENGTH(self)); if (right)
FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right);
if (PyUnicode_CopyCharacters(u, left,
(PyObject*)self, 0,
_PyUnicode_LENGTH(self)) < 0)
{
Py_DECREF(u);
return NULL;
} }
return (PyUnicodeObject*)u; return (PyUnicodeObject*)u;
...@@ -8821,8 +8849,12 @@ replace(PyObject *self, PyObject *str1, ...@@ -8821,8 +8849,12 @@ replace(PyObject *self, PyObject *str1,
u = PyUnicode_New(slen, maxchar); u = PyUnicode_New(slen, maxchar);
if (!u) if (!u)
goto error; goto error;
PyUnicode_CopyCharacters(u, 0, if (PyUnicode_CopyCharacters(u, 0,
(PyObject*)self, 0, slen); (PyObject*)self, 0, slen) < 0)
{
Py_DECREF(u);
return NULL;
}
rkind = PyUnicode_KIND(u); rkind = PyUnicode_KIND(u);
for (i = 0; i < PyUnicode_GET_LENGTH(u); i++) for (i = 0; i < PyUnicode_GET_LENGTH(u); i++)
if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) { if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) {
...@@ -9437,8 +9469,7 @@ PyUnicode_Concat(PyObject *left, PyObject *right) ...@@ -9437,8 +9469,7 @@ PyUnicode_Concat(PyObject *left, PyObject *right)
goto onError; goto onError;
maxchar = PyUnicode_MAX_CHAR_VALUE(u); maxchar = PyUnicode_MAX_CHAR_VALUE(u);
if (PyUnicode_MAX_CHAR_VALUE(v) > maxchar) maxchar = PY_MAX(maxchar, PyUnicode_MAX_CHAR_VALUE(v));
maxchar = PyUnicode_MAX_CHAR_VALUE(v);
/* Concat the two Unicode strings */ /* Concat the two Unicode strings */
w = PyUnicode_New( w = PyUnicode_New(
...@@ -9446,9 +9477,12 @@ PyUnicode_Concat(PyObject *left, PyObject *right) ...@@ -9446,9 +9477,12 @@ PyUnicode_Concat(PyObject *left, PyObject *right)
maxchar); maxchar);
if (w == NULL) if (w == NULL)
goto onError; goto onError;
PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)); if (PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u)) < 0)
PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), v, 0, goto onError;
PyUnicode_GET_LENGTH(v)); if (PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u),
v, 0,
PyUnicode_GET_LENGTH(v)) < 0)
goto onError;
Py_DECREF(u); Py_DECREF(u);
Py_DECREF(v); Py_DECREF(v);
return w; return w;
...@@ -10396,8 +10430,12 @@ substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len) ...@@ -10396,8 +10430,12 @@ substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len)
unicode = PyUnicode_New(len, maxchar); unicode = PyUnicode_New(len, maxchar);
if (unicode == NULL) if (unicode == NULL)
return NULL; return NULL;
PyUnicode_CopyCharacters(unicode, 0, if (PyUnicode_CopyCharacters(unicode, 0,
(PyObject*)self, start, len); (PyObject*)self, start, len) < 0)
{
Py_DECREF(unicode);
return NULL;
}
return unicode; return unicode;
} }
......
...@@ -245,8 +245,14 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident) ...@@ -245,8 +245,14 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident)
return 0; return 0;
/* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen); if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen); Py_DECREF(result);
return NULL;
}
if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
Py_DECREF(result);
return NULL;
}
return result; return result;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment