[Python-checkins] cpython (2.7): Issue #20434 Correct error handlin of _PyString_Resize and _PyBytes_Resize
kristjan.jonsson
python-checkins at python.org
Fri Apr 25 11:54:00 CEST 2014
http://hg.python.org/cpython/rev/4f79c3827adc
changeset: 90456:4f79c3827adc
branch: 2.7
parent: 90450:1beb3e0507fa
user: Kristján Valur Jónsson <sweskman at gmail.com>
date: Fri Apr 25 09:51:21 2014 +0000
summary:
Issue #20434 Correct error handlin of _PyString_Resize and _PyBytes_Resize
files:
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
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -549,14 +549,8 @@
}
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 @@
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 @@
}
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
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -320,12 +320,10 @@
}
*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 @@
** 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 @@
}
*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 @@
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 @@
}
}
}
- 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 @@
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 @@
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 @@
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
--- a/Modules/bz2module.c
+++ b/Modules/bz2module.c
@@ -732,7 +732,8 @@
}
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
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -994,10 +994,8 @@
*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
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -748,8 +748,8 @@
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
--- a/PC/_subprocess.c
+++ b/PC/_subprocess.c
@@ -367,7 +367,8 @@
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 @@
_PyString_Resize(&out, p - PyString_AS_STRING(out));
/* PyObject_Print(out, stdout, 0); */
-
+exit:
Py_XDECREF(keys);
Py_XDECREF(values);
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list