From be580f2e481f5f129a1e47e436dd7ae54b9f50bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 25 Apr 2014 09:51:21 +0000 Subject: Issue #20434 Correct error handlin of _PyString_Resize and _PyBytes_Resize --- Modules/_io/fileio.c | 15 +++---------- Modules/binascii.c | 55 +++++++++++++++++------------------------------ Modules/bz2module.c | 3 ++- Objects/bytearrayobject.c | 6 ++---- Objects/stringobject.c | 4 ++-- PC/_subprocess.c | 5 +++-- 6 files changed, 32 insertions(+), 56 deletions(-) diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 5946e6a..58b68b6 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -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; diff --git a/Modules/binascii.c b/Modules/binascii.c index 8334fe5..0b492d5 100644 --- a/Modules/binascii.c +++ b/Modules/binascii.c @@ -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; } diff --git a/Modules/bz2module.c b/Modules/bz2module.c index e479777..ae749ee 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -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; diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 21c58ce..fd0ce7c 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -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; } } diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 83dab08..0b6d36c 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -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); diff --git a/PC/_subprocess.c b/PC/_subprocess.c index 195e343..ffe8f41 100644 --- a/PC/_subprocess.c +++ b/PC/_subprocess.c @@ -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); -- cgit v0.12