static Py_ssize_t conn_recv_string(ConnectionObject *conn, char *buffer, size_t buflength, char **newbuffer, size_t maxlength) { int res; UINT32 ulength; *newbuffer = NULL; Py_BEGIN_ALLOW_THREADS res = _conn_recvall(conn->handle, (char*)&ulength, 4); Py_END_ALLOW_THREADS if (res < 0) return res; ulength = ntohl(ulength); if (ulength > maxlength) return MP_BAD_MESSAGE_LENGTH; if (ulength <= buflength) { Py_BEGIN_ALLOW_THREADS res = _conn_recvall(conn->handle, buffer, (size_t)ulength); Py_END_ALLOW_THREADS return res < 0 ? res : ulength; } else {
static Py_ssize_t conn_recv_string(ConnectionObject *conn, char *buffer, size_t buflength, char **newbuffer, size_t maxlength) { Py_ssize_t res; UINT32 ulength; *newbuffer = NULL; Py_BEGIN_ALLOW_THREADS res = _conn_recvall(conn->handle, (char*)&ulength, 4); Py_END_ALLOW_THREADS if (res < 0) return res; ulength = ntohl(ulength); if (ulength > maxlength) return MP_BAD_MESSAGE_LENGTH; if (ulength > buflength) { *newbuffer = buffer = PyMem_Malloc((size_t)ulength); if (buffer == NULL) return MP_MEMORY_ERROR; } Py_BEGIN_ALLOW_THREADS res = _conn_recvall(conn->handle, buffer, (size_t)ulength); Py_END_ALLOW_THREADS if (res >= 0) { res = (Py_ssize_t)ulength; } else if (*newbuffer != NULL) { PyMem_Free(*newbuffer); *newbuffer = NULL; } return res; }