diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -976,18 +976,19 @@ static PyObject * r = _bufferedreader_raw_read(self, PyBytes_AS_STRING(res), n); LEAVE_BUFFERED(self) if (r == -1) { Py_DECREF(res); return NULL; } if (r == -2) r = 0; - if (n > r) - _PyBytes_Resize(&res, r); + if (_PyBytes_Resize(&res, r)) { + return NULL; + } return res; } static PyObject * _buffered_readinto_generic(buffered *self, Py_buffer *buffer, char readinto1) { Py_ssize_t n, written = 0, remaining; PyObject *res = NULL; diff --git a/Modules/_multiprocessing/multiprocessing.c b/Modules/_multiprocessing/multiprocessing.c --- a/Modules/_multiprocessing/multiprocessing.c +++ b/Modules/_multiprocessing/multiprocessing.c @@ -85,17 +85,19 @@ multiprocessing_recv(PyObject *self, PyO Py_BEGIN_ALLOW_THREADS nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0); Py_END_ALLOW_THREADS if (nread < 0) { Py_DECREF(buf); return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); } - _PyBytes_Resize(&buf, nread); + if (_PyBytes_Resize(&buf, nread)) { + return NULL; + } return buf; } static PyObject * multiprocessing_send(PyObject *self, PyObject *args) { HANDLE handle; Py_buffer buf; diff --git a/Modules/_ssl.c b/Modules/_ssl.c --- a/Modules/_ssl.c +++ b/Modules/_ssl.c @@ -2175,17 +2175,19 @@ static PyObject * if (count <= 0) { PySSL_SetError(self, count, __FILE__, __LINE__); goto error; } done: Py_XDECREF(sock); if (!group_right_1) { - _PyBytes_Resize(&dest, count); + if (_PyBytes_Resize(&dest, count)) { + return NULL; + } return dest; } else { return PyLong_FromLong(count); } error: Py_XDECREF(sock); diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c --- a/Modules/ossaudiodev.c +++ b/Modules/ossaudiodev.c @@ -419,17 +419,19 @@ oss_read(oss_audio_t *self, PyObject *ar count = _Py_read(self->fd, PyBytes_AS_STRING(rv), size); if (count == -1) { Py_DECREF(rv); return NULL; } self->icount += count; - _PyBytes_Resize(&rv, count); + if (_PyBytes_Resize(&rv, count)) { + return NULL; + } return rv; } static PyObject * oss_write(oss_audio_t *self, PyObject *args) { Py_buffer data; Py_ssize_t rv; diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -7808,18 +7808,19 @@ os_read_impl(PyObject *module, int fd, P return NULL; n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); if (n == -1) { Py_DECREF(buffer); return NULL; } - if (n != length) - _PyBytes_Resize(&buffer, n); + if (_PyBytes_Resize(&buffer, n)) { + return NULL; + } return buffer; } #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) static Py_ssize_t iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) @@ -7975,18 +7976,19 @@ os_pread_impl(PyObject *module, int fd, _Py_END_SUPPRESS_IPH Py_END_ALLOW_THREADS } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); if (n < 0) { Py_DECREF(buffer); return (!async_err) ? posix_error() : NULL; } - if (n != length) - _PyBytes_Resize(&buffer, n); + if (_PyBytes_Resize(&buffer, n)) { + return NULL; + } return buffer; } #endif /* HAVE_PREAD */ /*[clinic input] os.write -> Py_ssize_t @@ -10546,19 +10548,19 @@ os_getxattr_impl(PyObject *module, path_ if (result < 0) { Py_DECREF(buffer); if (errno == ERANGE) continue; path_error(path); return NULL; } - if (result != buffer_size) { - /* Can only shrink. */ - _PyBytes_Resize(&buffer, result); + /* Can only shrink. */ + if (_PyBytes_Resize(&buffer, result)) { + return NULL; } break; } return buffer; } diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -2658,17 +2658,19 @@ sock_getsockopt(PySocketSockObject *s, P if (buf == NULL) return NULL; res = getsockopt(s->sock_fd, level, optname, (void *)PyBytes_AS_STRING(buf), &buflen); if (res < 0) { Py_DECREF(buf); return s->errorhandler(); } - _PyBytes_Resize(&buf, buflen); + if (_PyBytes_Resize(&buf, buflen)) { + return NULL; + } return buf; } PyDoc_STRVAR(getsockopt_doc, "getsockopt(level, option[, buffersize]) -> value\n\ \n\ Get a socket option. See the Unix manual for level and option.\n\ If a nonzero buffersize argument is given, the return value is a\n\ @@ -3080,20 +3082,20 @@ sock_recv(PySocketSockObject *s, PyObjec /* Call the guts */ outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags); if (outlen < 0) { /* An error occurred, release the string and return an error. */ Py_DECREF(buf); return NULL; } - if (outlen != recvlen) { - /* We did not read as many bytes as we anticipated, resize the - string if possible and be successful. */ - _PyBytes_Resize(&buf, outlen); + /* We did not read as many bytes as we anticipated, resize the + string if possible and be successful. */ + if (_PyBytes_Resize(&buf, outlen)) { + return NULL; } return buf; } PyDoc_STRVAR(recv_doc, "recv(buffersize[, flags]) -> data\n\ \n\ @@ -3508,17 +3510,19 @@ err_closefds: static PyObject * makeval_recvmsg(ssize_t received, void *data) { PyObject **buf = data; if (received < PyBytes_GET_SIZE(*buf)) - _PyBytes_Resize(buf, received); + if (_PyBytes_Resize(buf, received)) { + return NULL; + } Py_XINCREF(*buf); return *buf; } /* s.recvmsg(bufsize[, ancbufsize[, flags]]) method */ static PyObject * sock_recvmsg(PySocketSockObject *s, PyObject *args)