Issue #20434 Correct error handlin of _PyString_Resize and _PyBytes_Resize
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 @@
}
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;