bpo-35436: Add missing PyErr_NoMemory() calls and other minor bug fixes. (GH-11015) (GH-11020)
(cherry picked from commit 4c49da0cb7434c676d70b9ccf38aca82ac0d64a9)
diff --git a/Parser/myreadline.c b/Parser/myreadline.c
index edb291a..58dc0f7 100644
--- a/Parser/myreadline.c
+++ b/Parser/myreadline.c
@@ -153,20 +153,37 @@
wbuf = (wchar_t*)PyMem_RawMalloc(wbuflen * sizeof(wchar_t));
if (wbuf)
wcscpy_s(wbuf, wbuflen, wbuf_local);
+ else {
+ PyErr_NoMemory();
+ goto exit;
+ }
}
- else
- wbuf = (wchar_t*)PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t));
+ else {
+ wchar_t *tmp = PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t));
+ if (tmp == NULL) {
+ PyErr_NoMemory();
+ goto exit;
+ }
+ wbuf = tmp;
+ }
}
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 @@
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 @@
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 @@
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 @@
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;