Issue #20434 Correct error handlin of _PyString_Resize and _PyBytes_Resize

parent 4e0df17b
......@@ -549,14 +549,8 @@ fileio_readall(fileio *self)
}
if (PyBytes_GET_SIZE(result) < (Py_ssize_t)newsize) {
if (_PyBytes_Resize(&result, newsize) < 0) {
if (total == 0) {
Py_DECREF(result);
return NULL;
}
PyErr_Clear();
break;
}
if (_PyBytes_Resize(&result, newsize) < 0)
return NULL; /* result has been freed */
}
Py_BEGIN_ALLOW_THREADS
errno = 0;
......@@ -599,7 +593,6 @@ fileio_readall(fileio *self)
if (PyBytes_GET_SIZE(result) > total) {
if (_PyBytes_Resize(&result, total) < 0) {
/* This should never happen, but just in case */
Py_DECREF(result);
return NULL;
}
}
......@@ -656,10 +649,8 @@ fileio_read(fileio *self, PyObject *args)
}
if (n != size) {
if (_PyBytes_Resize(&bytes, n) < 0) {
Py_DECREF(bytes);
if (_PyBytes_Resize(&bytes, n) < 0)
return NULL;
}
}
return (PyObject *) bytes;
......
......@@ -320,12 +320,10 @@ binascii_b2a_uu(PyObject *self, PyObject *args)
}
*ascii_data++ = '\n'; /* Append a courtesy newline */
if (_PyString_Resize(&rv,
/* rv is cleared on error */
(void)_PyString_Resize(&rv,
(ascii_data -
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
Py_DECREF(rv);
rv = NULL;
}
(unsigned char *)PyString_AS_STRING(rv)));
PyBuffer_Release(&pbin);
return rv;
}
......@@ -452,10 +450,8 @@ binascii_a2b_base64(PyObject *self, PyObject *args)
** string instead; _PyString_Resize() won't do this for us.
*/
if (bin_len > 0) {
if (_PyString_Resize(&rv, bin_len) < 0) {
Py_DECREF(rv);
rv = NULL;
}
/* rv is cleared on error */
(void)_PyString_Resize(&rv, bin_len);
}
else {
Py_DECREF(rv);
......@@ -522,12 +518,10 @@ binascii_b2a_base64(PyObject *self, PyObject *args)
}
*ascii_data++ = '\n'; /* Append a courtesy newline */
if (_PyString_Resize(&rv,
/* rv is cleared on error */
(void)_PyString_Resize(&rv,
(ascii_data -
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
Py_DECREF(rv);
rv = NULL;
}
(unsigned char *)PyString_AS_STRING(rv)));
PyBuffer_Release(&pbuf);
return rv;
}
......@@ -601,13 +595,10 @@ binascii_a2b_hqx(PyObject *self, PyObject *args)
Py_DECREF(rv);
return NULL;
}
/* rv is cleared on error */
if (_PyString_Resize(&rv,
(bin_data -
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
Py_DECREF(rv);
rv = NULL;
}
if (rv) {
(unsigned char *)PyString_AS_STRING(rv))) == 0) {
PyObject *rrv = Py_BuildValue("Oi", rv, done);
PyBuffer_Release(&pascii);
Py_DECREF(rv);
......@@ -672,12 +663,10 @@ binascii_rlecode_hqx(PyObject *self, PyObject *args)
}
}
}
if (_PyString_Resize(&rv,
/* rv is cleared on error */
(void)_PyString_Resize(&rv,
(out_data -
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
Py_DECREF(rv);
rv = NULL;
}
(unsigned char *)PyString_AS_STRING(rv)));
PyBuffer_Release(&pbuf);
return rv;
}
......@@ -729,12 +718,10 @@ binascii_b2a_hqx(PyObject *self, PyObject *args)
leftchar <<= (6-leftbits);
*ascii_data++ = table_b2a_hqx[leftchar & 0x3f];
}
if (_PyString_Resize(&rv,
/* rv is cleared on error */
(void)_PyString_Resize(&rv,
(ascii_data -
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
Py_DECREF(rv);
rv = NULL;
}
(unsigned char *)PyString_AS_STRING(rv)));
PyBuffer_Release(&pbin);
return rv;
}
......@@ -796,7 +783,7 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args)
if ( --out_len_left < 0 ) { \
if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
if (_PyString_Resize(&rv, 2*out_len) < 0) \
{ Py_DECREF(rv); PyBuffer_Release(&pin); return NULL; } \
{ PyBuffer_Release(&pin); return NULL; } \
out_data = (unsigned char *)PyString_AS_STRING(rv) \
+ out_len; \
out_len_left = out_len-1; \
......@@ -846,12 +833,10 @@ binascii_rledecode_hqx(PyObject *self, PyObject *args)
OUTBYTE(in_byte);
}
}
if (_PyString_Resize(&rv,
/* rv is cleared on error */
(void)_PyString_Resize(&rv,
(out_data -
(unsigned char *)PyString_AS_STRING(rv))) < 0) {
Py_DECREF(rv);
rv = NULL;
}
(unsigned char *)PyString_AS_STRING(rv)));
PyBuffer_Release(&pin);
return rv;
}
......
......@@ -732,7 +732,8 @@ BZ2File_readlines(BZ2FileObject *self, PyObject *args)
}
else {
/* Grow the big buffer */
_PyString_Resize(&big_buffer, buffersize);
if (_PyString_Resize(&big_buffer, buffersize))
goto error;
buffer = PyString_AS_STRING(big_buffer);
}
continue;
......
......@@ -994,10 +994,8 @@ bytearray_repr(PyByteArrayObject *self)
*p++ = *quote_postfix++;
}
*p = '\0';
if (_PyString_Resize(&v, (p - PyString_AS_STRING(v)))) {
Py_DECREF(v);
return NULL;
}
/* v is cleared on error */
(void)_PyString_Resize(&v, (p - PyString_AS_STRING(v)));
return v;
}
}
......
......@@ -748,8 +748,8 @@ PyObject *PyString_DecodeEscape(const char *s,
UTF-8 bytes may follow. */
}
}
if (p-buf < newlen && _PyString_Resize(&v, p - buf))
goto failed;
if (p-buf < newlen)
_PyString_Resize(&v, p - buf); /* v is cleared on error */
return v;
failed:
Py_DECREF(v);
......
......@@ -367,7 +367,8 @@ getenvironment(PyObject* environment)
vsize + 1 + 1;
if (totalsize > PyString_GET_SIZE(out)) {
int offset = p - PyString_AS_STRING(out);
_PyString_Resize(&out, totalsize + 1024);
if (_PyString_Resize(&out, totalsize + 1024))
goto exit;
p = PyString_AS_STRING(out) + offset;
}
memcpy(p, PyString_AS_STRING(key), ksize);
......@@ -383,7 +384,7 @@ getenvironment(PyObject* environment)
_PyString_Resize(&out, p - PyString_AS_STRING(out));
/* PyObject_Print(out, stdout, 0); */
exit:
Py_XDECREF(keys);
Py_XDECREF(values);
......
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