Commit 4c49da0c authored by Zackery Spytz's avatar Zackery Spytz Committed by Serhiy Storchaka

bpo-35436: Add missing PyErr_NoMemory() calls and other minor bug fixes. (GH-11015)

Set MemoryError when appropriate, add missing failure checks,
and fix some potential leaks.
parent 3a521f0b
Fix various issues with memory allocation error handling. Patch by Zackery
Spytz.
...@@ -728,6 +728,10 @@ subclasscheck_check_registry(_abc_data *impl, PyObject *subclass, ...@@ -728,6 +728,10 @@ subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
// Weakref callback may remove entry from set. // Weakref callback may remove entry from set.
// So we take snapshot of registry first. // So we take snapshot of registry first.
PyObject **copy = PyMem_Malloc(sizeof(PyObject*) * registry_size); PyObject **copy = PyMem_Malloc(sizeof(PyObject*) * registry_size);
if (copy == NULL) {
PyErr_NoMemory();
return -1;
}
PyObject *key; PyObject *key;
Py_ssize_t pos = 0; Py_ssize_t pos = 0;
Py_hash_t hash; Py_hash_t hash;
......
...@@ -305,8 +305,10 @@ _ctypes_alloc_format_string_for_type(char code, int big_endian) ...@@ -305,8 +305,10 @@ _ctypes_alloc_format_string_for_type(char code, int big_endian)
} }
result = PyMem_Malloc(3); result = PyMem_Malloc(3);
if (result == NULL) if (result == NULL) {
PyErr_NoMemory();
return NULL; return NULL;
}
result[0] = big_endian ? '>' : '<'; result[0] = big_endian ? '>' : '<';
result[1] = pep_code; result[1] = pep_code;
...@@ -366,8 +368,10 @@ _ctypes_alloc_format_string_with_shape(int ndim, const Py_ssize_t *shape, ...@@ -366,8 +368,10 @@ _ctypes_alloc_format_string_with_shape(int ndim, const Py_ssize_t *shape,
if (prefix) if (prefix)
prefix_len += strlen(prefix); prefix_len += strlen(prefix);
new_prefix = PyMem_Malloc(prefix_len); new_prefix = PyMem_Malloc(prefix_len);
if (new_prefix == NULL) if (new_prefix == NULL) {
PyErr_NoMemory();
return NULL; return NULL;
}
new_prefix[0] = '\0'; new_prefix[0] = '\0';
if (prefix) if (prefix)
strcpy(new_prefix, prefix); strcpy(new_prefix, prefix);
...@@ -1899,6 +1903,10 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject ...@@ -1899,6 +1903,10 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
#else #else
suffix = PyUnicode_InternFromString("_be"); suffix = PyUnicode_InternFromString("_be");
#endif #endif
if (suffix == NULL) {
Py_DECREF(swapped_args);
return NULL;
}
newname = PyUnicode_Concat(name, suffix); newname = PyUnicode_Concat(name, suffix);
if (newname == NULL) { if (newname == NULL) {
......
...@@ -310,7 +310,6 @@ static CThunkObject* CThunkObject_new(Py_ssize_t nArgs) ...@@ -310,7 +310,6 @@ static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs); p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs);
if (p == NULL) { if (p == NULL) {
PyErr_NoMemory();
return NULL; return NULL;
} }
......
...@@ -816,11 +816,13 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self) ...@@ -816,11 +816,13 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self)
} }
bufsize = newsize; bufsize = newsize;
buf = PyMem_Realloc(buf, (bufsize + 1) * sizeof(wchar_t)); wchar_t *tmp = PyMem_Realloc(buf,
if (!buf) { (bufsize + 1) * sizeof(wchar_t));
if (tmp == NULL) {
PyMem_Free(buf); PyMem_Free(buf);
return NULL; return NULL;
} }
buf = tmp;
} }
subbuf = read_console_w(self->handle, bufsize - len, &n); subbuf = read_console_w(self->handle, bufsize - len, &n);
......
...@@ -449,8 +449,9 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -449,8 +449,9 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (!unlink) { if (!unlink) {
name_copy = PyMem_Malloc(strlen(name) + 1); name_copy = PyMem_Malloc(strlen(name) + 1);
if (name_copy == NULL) if (name_copy == NULL) {
goto failure; return PyErr_NoMemory();
}
strcpy(name_copy, name); strcpy(name_copy, name);
} }
...@@ -473,7 +474,9 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -473,7 +474,9 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (handle != SEM_FAILED) if (handle != SEM_FAILED)
SEM_CLOSE(handle); SEM_CLOSE(handle);
PyMem_Free(name_copy); PyMem_Free(name_copy);
_PyMp_SetError(NULL, MP_STANDARD_ERROR); if (!PyErr_Occurred()) {
_PyMp_SetError(NULL, MP_STANDARD_ERROR);
}
return NULL; return NULL;
} }
......
...@@ -911,6 +911,11 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock, ...@@ -911,6 +911,11 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,
PySSL_BEGIN_ALLOW_THREADS PySSL_BEGIN_ALLOW_THREADS
self->ssl = SSL_new(ctx); self->ssl = SSL_new(ctx);
PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS
if (self->ssl == NULL) {
Py_DECREF(self);
_setSSLError(NULL, 0, __FILE__, __LINE__);
return NULL;
}
SSL_set_app_data(self->ssl, self); SSL_set_app_data(self->ssl, self);
if (sock) { if (sock) {
SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int)); SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int));
...@@ -1240,6 +1245,10 @@ _get_peer_alt_names (X509 *certificate) { ...@@ -1240,6 +1245,10 @@ _get_peer_alt_names (X509 *certificate) {
/* get a memory buffer */ /* get a memory buffer */
biobuf = BIO_new(BIO_s_mem()); biobuf = BIO_new(BIO_s_mem());
if (biobuf == NULL) {
PyErr_SetString(PySSLErrorObject, "failed to allocate BIO");
return NULL;
}
names = (GENERAL_NAMES *)X509_get_ext_d2i( names = (GENERAL_NAMES *)X509_get_ext_d2i(
certificate, NID_subject_alt_name, NULL, NULL); certificate, NID_subject_alt_name, NULL, NULL);
...@@ -1592,6 +1601,10 @@ _decode_certificate(X509 *certificate) { ...@@ -1592,6 +1601,10 @@ _decode_certificate(X509 *certificate) {
/* get a memory buffer */ /* get a memory buffer */
biobuf = BIO_new(BIO_s_mem()); biobuf = BIO_new(BIO_s_mem());
if (biobuf == NULL) {
PyErr_SetString(PySSLErrorObject, "failed to allocate BIO");
goto fail0;
}
(void) BIO_reset(biobuf); (void) BIO_reset(biobuf);
serialNumber = X509_get_serialNumber(certificate); serialNumber = X509_get_serialNumber(certificate);
......
...@@ -2142,7 +2142,7 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q) ...@@ -2142,7 +2142,7 @@ math_dist_impl(PyObject *module, PyObject *p, PyObject *q)
if (n > NUM_STACK_ELEMS) { if (n > NUM_STACK_ELEMS) {
diffs = (double *) PyObject_Malloc(n * sizeof(double)); diffs = (double *) PyObject_Malloc(n * sizeof(double));
if (diffs == NULL) { if (diffs == NULL) {
return NULL; return PyErr_NoMemory();
} }
} }
for (i=0 ; i<n ; i++) { for (i=0 ; i<n ; i++) {
...@@ -2199,8 +2199,9 @@ math_hypot(PyObject *self, PyObject *args) ...@@ -2199,8 +2199,9 @@ math_hypot(PyObject *self, PyObject *args)
n = PyTuple_GET_SIZE(args); n = PyTuple_GET_SIZE(args);
if (n > NUM_STACK_ELEMS) { if (n > NUM_STACK_ELEMS) {
coordinates = (double *) PyObject_Malloc(n * sizeof(double)); coordinates = (double *) PyObject_Malloc(n * sizeof(double));
if (coordinates == NULL) if (coordinates == NULL) {
return NULL; return PyErr_NoMemory();
}
} }
for (i=0 ; i<n ; i++) { for (i=0 ; i<n ; i++) {
item = PyTuple_GET_ITEM(args, i); item = PyTuple_GET_ITEM(args, i);
......
...@@ -6651,8 +6651,7 @@ os_getgroups_impl(PyObject *module) ...@@ -6651,8 +6651,7 @@ os_getgroups_impl(PyObject *module)
} else { } else {
alt_grouplist = PyMem_New(gid_t, n); alt_grouplist = PyMem_New(gid_t, n);
if (alt_grouplist == NULL) { if (alt_grouplist == NULL) {
errno = EINVAL; return PyErr_NoMemory();
return posix_error();
} }
} }
...@@ -6677,8 +6676,7 @@ os_getgroups_impl(PyObject *module) ...@@ -6677,8 +6676,7 @@ os_getgroups_impl(PyObject *module)
} else { } else {
alt_grouplist = PyMem_New(gid_t, n); alt_grouplist = PyMem_New(gid_t, n);
if (alt_grouplist == NULL) { if (alt_grouplist == NULL) {
errno = EINVAL; return PyErr_NoMemory();
return posix_error();
} }
n = getgroups(n, alt_grouplist); n = getgroups(n, alt_grouplist);
if (n == -1) { if (n == -1) {
......
...@@ -201,7 +201,7 @@ PyCapsule_Import(const char *name, int no_block) ...@@ -201,7 +201,7 @@ PyCapsule_Import(const char *name, int no_block)
char *name_dup = (char *)PyMem_MALLOC(name_length); char *name_dup = (char *)PyMem_MALLOC(name_length);
if (!name_dup) { if (!name_dup) {
return NULL; return PyErr_NoMemory();
} }
memcpy(name_dup, name, name_length); memcpy(name_dup, name, name_length);
......
...@@ -575,6 +575,9 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path) ...@@ -575,6 +575,9 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path)
size_t prefixlen = wcslen(prefix); size_t prefixlen = wcslen(prefix);
wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t)); wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
if (buf == NULL) {
goto error;
}
buf[0] = '\0'; buf[0] = '\0';
while (!feof(sp_file)) { while (!feof(sp_file)) {
...@@ -603,17 +606,22 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path) ...@@ -603,17 +606,22 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path)
DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0); DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t)); wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
if (wline == NULL) {
goto error;
}
wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1); wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
wline[wn] = '\0'; wline[wn] = '\0';
size_t usedsiz = wcslen(buf); size_t usedsiz = wcslen(buf);
while (usedsiz + wn + prefixlen + 4 > bufsiz) { while (usedsiz + wn + prefixlen + 4 > bufsiz) {
bufsiz += MAXPATHLEN; bufsiz += MAXPATHLEN;
buf = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) * sizeof(wchar_t)); wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
if (!buf) { sizeof(wchar_t));
if (tmp == NULL) {
PyMem_RawFree(wline); PyMem_RawFree(wline);
goto error; goto error;
} }
buf = tmp;
} }
if (usedsiz) { if (usedsiz) {
......
...@@ -1763,6 +1763,9 @@ process(int argc, wchar_t ** argv) ...@@ -1763,6 +1763,9 @@ process(int argc, wchar_t ** argv)
} }
cch += (DWORD)wcslen(PYTHON_EXECUTABLE) + 1 + 1; /* include sep and null */ cch += (DWORD)wcslen(PYTHON_EXECUTABLE) + 1 + 1; /* include sep and null */
executable = (wchar_t *)malloc(cch * sizeof(wchar_t)); executable = (wchar_t *)malloc(cch * sizeof(wchar_t));
if (executable == NULL) {
error(RC_NO_MEMORY, L"A memory allocation failed");
}
cch_actual = MultiByteToWideChar(CP_UTF8, 0, start, len, executable, cch); cch_actual = MultiByteToWideChar(CP_UTF8, 0, start, len, executable, cch);
if (!cch_actual) { if (!cch_actual) {
error(RC_BAD_VENV_CFG, L"Cannot decode home path in '%ls'", error(RC_BAD_VENV_CFG, L"Cannot decode home path in '%ls'",
......
...@@ -153,20 +153,37 @@ _PyOS_WindowsConsoleReadline(HANDLE hStdIn) ...@@ -153,20 +153,37 @@ _PyOS_WindowsConsoleReadline(HANDLE hStdIn)
wbuf = (wchar_t*)PyMem_RawMalloc(wbuflen * sizeof(wchar_t)); wbuf = (wchar_t*)PyMem_RawMalloc(wbuflen * sizeof(wchar_t));
if (wbuf) if (wbuf)
wcscpy_s(wbuf, wbuflen, wbuf_local); wcscpy_s(wbuf, wbuflen, wbuf_local);
else {
PyErr_NoMemory();
goto exit;
}
}
else {
wchar_t *tmp = PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t));
if (tmp == NULL) {
PyErr_NoMemory();
goto exit;
}
wbuf = tmp;
} }
else
wbuf = (wchar_t*)PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t));
} }
if (wbuf[0] == '\x1a') { if (wbuf[0] == '\x1a') {
buf = PyMem_RawMalloc(1); buf = PyMem_RawMalloc(1);
if (buf) if (buf)
buf[0] = '\0'; buf[0] = '\0';
else {
PyErr_NoMemory();
}
goto exit; goto exit;
} }
u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, NULL, 0, NULL, NULL); u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, NULL, 0, NULL, NULL);
buf = PyMem_RawMalloc(u8len + 1); buf = PyMem_RawMalloc(u8len + 1);
if (buf == NULL) {
PyErr_NoMemory();
goto exit;
}
u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, buf, u8len, NULL, NULL); u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, buf, u8len, NULL, NULL);
buf[u8len] = '\0'; buf[u8len] = '\0';
...@@ -211,8 +228,12 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) ...@@ -211,8 +228,12 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
int wlen; int wlen;
wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1, wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
NULL, 0); NULL, 0);
if (wlen && if (wlen) {
(wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t)))) { wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t));
if (wbuf == NULL) {
PyErr_NoMemory();
return NULL;
}
wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1, wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
wbuf, wlen); wbuf, wlen);
if (wlen) { if (wlen) {
...@@ -236,8 +257,10 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) ...@@ -236,8 +257,10 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
n = 100; n = 100;
p = (char *)PyMem_RawMalloc(n); p = (char *)PyMem_RawMalloc(n);
if (p == NULL) if (p == NULL) {
PyErr_NoMemory();
return NULL; return NULL;
}
fflush(sys_stdout); fflush(sys_stdout);
if (prompt) if (prompt)
...@@ -314,6 +337,10 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) ...@@ -314,6 +337,10 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
if (_PyOS_ReadlineLock == NULL) { if (_PyOS_ReadlineLock == NULL) {
_PyOS_ReadlineLock = PyThread_allocate_lock(); _PyOS_ReadlineLock = PyThread_allocate_lock();
if (_PyOS_ReadlineLock == NULL) {
PyErr_SetString(PyExc_MemoryError, "can't allocate lock");
return NULL;
}
} }
_PyOS_ReadlineTState = _PyThreadState_GET(); _PyOS_ReadlineTState = _PyThreadState_GET();
...@@ -341,8 +368,12 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) ...@@ -341,8 +368,12 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
len = strlen(rv) + 1; len = strlen(rv) + 1;
res = PyMem_Malloc(len); res = PyMem_Malloc(len);
if (res != NULL) if (res != NULL) {
memcpy(res, rv, len); memcpy(res, rv, len);
}
else {
PyErr_NoMemory();
}
PyMem_RawFree(rv); PyMem_RawFree(rv);
return res; return res;
......
...@@ -953,6 +953,11 @@ tok_nextc(struct tok_state *tok) ...@@ -953,6 +953,11 @@ tok_nextc(struct tok_state *tok)
buflen = PyBytes_GET_SIZE(u); buflen = PyBytes_GET_SIZE(u);
buf = PyBytes_AS_STRING(u); buf = PyBytes_AS_STRING(u);
newtok = PyMem_MALLOC(buflen+1); newtok = PyMem_MALLOC(buflen+1);
if (newtok == NULL) {
Py_DECREF(u);
tok->done = E_NOMEM;
return EOF;
}
strcpy(newtok, buf); strcpy(newtok, buf);
Py_DECREF(u); Py_DECREF(u);
} }
......
...@@ -4097,6 +4097,9 @@ parsenumber(struct compiling *c, const char *s) ...@@ -4097,6 +4097,9 @@ parsenumber(struct compiling *c, const char *s)
} }
/* Create a duplicate without underscores. */ /* Create a duplicate without underscores. */
dup = PyMem_Malloc(strlen(s) + 1); dup = PyMem_Malloc(strlen(s) + 1);
if (dup == NULL) {
return PyErr_NoMemory();
}
end = dup; end = dup;
for (; *s; s++) { for (; *s; s++) {
if (*s != '_') { if (*s != '_') {
...@@ -4325,8 +4328,10 @@ fstring_compile_expr(const char *expr_start, const char *expr_end, ...@@ -4325,8 +4328,10 @@ fstring_compile_expr(const char *expr_start, const char *expr_end,
len = expr_end - expr_start; len = expr_end - expr_start;
/* Allocate 3 extra bytes: open paren, close paren, null byte. */ /* Allocate 3 extra bytes: open paren, close paren, null byte. */
str = PyMem_RawMalloc(len + 3); str = PyMem_RawMalloc(len + 3);
if (str == NULL) if (str == NULL) {
PyErr_NoMemory();
return NULL; return NULL;
}
str[0] = '('; str[0] = '(';
memcpy(str+1, expr_start, len); memcpy(str+1, expr_start, len);
......
...@@ -670,11 +670,12 @@ r_string(Py_ssize_t n, RFILE *p) ...@@ -670,11 +670,12 @@ r_string(Py_ssize_t n, RFILE *p)
p->buf_size = n; p->buf_size = n;
} }
else if (p->buf_size < n) { else if (p->buf_size < n) {
p->buf = PyMem_REALLOC(p->buf, n); char *tmp = PyMem_REALLOC(p->buf, n);
if (p->buf == NULL) { if (tmp == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
p->buf = tmp;
p->buf_size = n; p->buf_size = n;
} }
......
...@@ -398,6 +398,9 @@ _Py_string_to_number_with_underscores( ...@@ -398,6 +398,9 @@ _Py_string_to_number_with_underscores(
} }
dup = PyMem_Malloc(orig_len + 1); dup = PyMem_Malloc(orig_len + 1);
if (dup == NULL) {
return PyErr_NoMemory();
}
end = dup; end = dup;
prev = '\0'; prev = '\0';
last = s + orig_len; last = s + orig_len;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment