@@ -162,20 +162,37 @@ _PyOS_WindowsConsoleReadline(HANDLE hStdIn)
162162 wbuf = (wchar_t * )PyMem_RawMalloc (wbuflen * sizeof (wchar_t ));
163163 if (wbuf )
164164 wcscpy_s (wbuf , wbuflen , wbuf_local );
165+ else {
166+ PyErr_NoMemory ();
167+ goto exit ;
168+ }
169+ }
170+ else {
171+ wchar_t * tmp = PyMem_RawRealloc (wbuf , wbuflen * sizeof (wchar_t ));
172+ if (tmp == NULL ) {
173+ PyErr_NoMemory ();
174+ goto exit ;
175+ }
176+ wbuf = tmp ;
165177 }
166- else
167- wbuf = (wchar_t * )PyMem_RawRealloc (wbuf , wbuflen * sizeof (wchar_t ));
168178 }
169179
170180 if (wbuf [0 ] == '\x1a' ) {
171181 buf = PyMem_RawMalloc (1 );
172182 if (buf )
173183 buf [0 ] = '\0' ;
184+ else {
185+ PyErr_NoMemory ();
186+ }
174187 goto exit ;
175188 }
176189
177190 u8len = WideCharToMultiByte (CP_UTF8 , 0 , wbuf , total_read , NULL , 0 , NULL , NULL );
178191 buf = PyMem_RawMalloc (u8len + 1 );
192+ if (buf == NULL ) {
193+ PyErr_NoMemory ();
194+ goto exit ;
195+ }
179196 u8len = WideCharToMultiByte (CP_UTF8 , 0 , wbuf , total_read , buf , u8len , NULL , NULL );
180197 buf [u8len ] = '\0' ;
181198
@@ -224,8 +241,12 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
224241 int wlen ;
225242 wlen = MultiByteToWideChar (CP_UTF8 , 0 , prompt , -1 ,
226243 NULL , 0 );
227- if (wlen &&
228- (wbuf = PyMem_RawMalloc (wlen * sizeof (wchar_t )))) {
244+ if (wlen ) {
245+ wbuf = PyMem_RawMalloc (wlen * sizeof (wchar_t ));
246+ if (wbuf == NULL ) {
247+ PyErr_NoMemory ();
248+ return NULL ;
249+ }
229250 wlen = MultiByteToWideChar (CP_UTF8 , 0 , prompt , -1 ,
230251 wbuf , wlen );
231252 if (wlen ) {
@@ -249,8 +270,10 @@ PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
249270
250271 n = 100 ;
251272 p = (char * )PyMem_RawMalloc (n );
252- if (p == NULL )
273+ if (p == NULL ) {
274+ PyErr_NoMemory ();
253275 return NULL ;
276+ }
254277
255278 fflush (sys_stdout );
256279 if (prompt )
@@ -328,6 +351,10 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
328351#ifdef WITH_THREAD
329352 if (_PyOS_ReadlineLock == NULL ) {
330353 _PyOS_ReadlineLock = PyThread_allocate_lock ();
354+ if (_PyOS_ReadlineLock == NULL ) {
355+ PyErr_SetString (PyExc_MemoryError , "can't allocate lock" );
356+ return NULL ;
357+ }
331358 }
332359#endif
333360
@@ -360,8 +387,12 @@ PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
360387
361388 len = strlen (rv ) + 1 ;
362389 res = PyMem_Malloc (len );
363- if (res != NULL )
390+ if (res != NULL ) {
364391 memcpy (res , rv , len );
392+ }
393+ else {
394+ PyErr_NoMemory ();
395+ }
365396 PyMem_RawFree (rv );
366397
367398 return res ;
0 commit comments