diff options
author | Zackery Spytz <zspytz@gmail.com> | 2018-12-07 10:11:30 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2018-12-07 10:11:30 (GMT) |
commit | 4c49da0cb7434c676d70b9ccf38aca82ac0d64a9 (patch) | |
tree | aae3660f9a5bc462830107cf2311b2557898e268 /Parser | |
parent | 3a521f0b6167628f975c773b56c7daf8d33d6f40 (diff) | |
download | cpython-4c49da0cb7434c676d70b9ccf38aca82ac0d64a9.zip cpython-4c49da0cb7434c676d70b9ccf38aca82ac0d64a9.tar.gz cpython-4c49da0cb7434c676d70b9ccf38aca82ac0d64a9.tar.bz2 |
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.
Diffstat (limited to 'Parser')
-rw-r--r-- | Parser/myreadline.c | 43 | ||||
-rw-r--r-- | Parser/tokenizer.c | 5 |
2 files changed, 42 insertions, 6 deletions
diff --git a/Parser/myreadline.c b/Parser/myreadline.c index acb4d01..43e5583 100644 --- a/Parser/myreadline.c +++ b/Parser/myreadline.c @@ -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; diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index fc75bae..d319a4c 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -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); } |