mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
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.
This commit is contained in:
parent
3a521f0b61
commit
4c49da0cb7
17 changed files with 113 additions and 27 deletions
|
|
@ -153,20 +153,37 @@ _PyOS_WindowsConsoleReadline(HANDLE hStdIn)
|
|||
wbuf = (wchar_t*)PyMem_RawMalloc(wbuflen * sizeof(wchar_t));
|
||||
if (wbuf)
|
||||
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') {
|
||||
buf = PyMem_RawMalloc(1);
|
||||
if (buf)
|
||||
buf[0] = '\0';
|
||||
else {
|
||||
PyErr_NoMemory();
|
||||
}
|
||||
goto exit;
|
||||
}
|
||||
|
||||
u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, NULL, 0, NULL, NULL);
|
||||
buf = PyMem_RawMalloc(u8len + 1);
|
||||
if (buf == NULL) {
|
||||
PyErr_NoMemory();
|
||||
goto exit;
|
||||
}
|
||||
u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, buf, u8len, NULL, NULL);
|
||||
buf[u8len] = '\0';
|
||||
|
||||
|
|
@ -211,8 +228,12 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
|
|||
int wlen;
|
||||
wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
|
||||
NULL, 0);
|
||||
if (wlen &&
|
||||
(wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t)))) {
|
||||
if (wlen) {
|
||||
wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t));
|
||||
if (wbuf == NULL) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
|
||||
wbuf, wlen);
|
||||
if (wlen) {
|
||||
|
|
@ -236,8 +257,10 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
|
|||
|
||||
n = 100;
|
||||
p = (char *)PyMem_RawMalloc(n);
|
||||
if (p == NULL)
|
||||
if (p == NULL) {
|
||||
PyErr_NoMemory();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fflush(sys_stdout);
|
||||
if (prompt)
|
||||
|
|
@ -314,6 +337,10 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
|
|||
|
||||
if (_PyOS_ReadlineLock == NULL) {
|
||||
_PyOS_ReadlineLock = PyThread_allocate_lock();
|
||||
if (_PyOS_ReadlineLock == NULL) {
|
||||
PyErr_SetString(PyExc_MemoryError, "can't allocate lock");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
_PyOS_ReadlineTState = _PyThreadState_GET();
|
||||
|
|
@ -341,8 +368,12 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
|
|||
|
||||
len = strlen(rv) + 1;
|
||||
res = PyMem_Malloc(len);
|
||||
if (res != NULL)
|
||||
if (res != NULL) {
|
||||
memcpy(res, rv, len);
|
||||
}
|
||||
else {
|
||||
PyErr_NoMemory();
|
||||
}
|
||||
PyMem_RawFree(rv);
|
||||
|
||||
return res;
|
||||
|
|
|
|||
|
|
@ -953,6 +953,11 @@ tok_nextc(struct tok_state *tok)
|
|||
buflen = PyBytes_GET_SIZE(u);
|
||||
buf = PyBytes_AS_STRING(u);
|
||||
newtok = PyMem_MALLOC(buflen+1);
|
||||
if (newtok == NULL) {
|
||||
Py_DECREF(u);
|
||||
tok->done = E_NOMEM;
|
||||
return EOF;
|
||||
}
|
||||
strcpy(newtok, buf);
|
||||
Py_DECREF(u);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue