Skip to content

Commit bf0fc39

Browse files
committed
Issue #29392: Prevent crash when passing invalid arguments into msvcrt module.
2 parents 1add96f + 21fae03 commit bf0fc39

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ Library
146146
Windows
147147
-------
148148

149+
- Issue #29392: Prevent crash when passing invalid arguments into msvcrt module.
150+
149151
- Issue #25778: winreg does not truncate string correctly (Patch by Eryk Sun)
150152

151153
- Issue #28896: Deprecate WindowsRegistryFinder and disable it by default.

PC/msvcrtmodule.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ msvcrt_locking_impl(PyObject *module, int fd, int mode, long nbytes)
111111
int err;
112112

113113
Py_BEGIN_ALLOW_THREADS
114+
_Py_BEGIN_SUPPRESS_IPH
114115
err = _locking(fd, mode, nbytes);
116+
_Py_END_SUPPRESS_IPH
115117
Py_END_ALLOW_THREADS
116118
if (err != 0)
117119
return PyErr_SetFromErrno(PyExc_IOError);
@@ -138,7 +140,9 @@ static long
138140
msvcrt_setmode_impl(PyObject *module, int fd, int flags)
139141
/*[clinic end generated code: output=24a9be5ea07ccb9b input=76e7c01f6b137f75]*/
140142
{
143+
_Py_BEGIN_SUPPRESS_IPH
141144
flags = _setmode(fd, flags);
145+
_Py_END_SUPPRESS_IPH
142146
if (flags == -1)
143147
PyErr_SetFromErrno(PyExc_IOError);
144148

@@ -165,7 +169,9 @@ msvcrt_open_osfhandle_impl(PyObject *module, intptr_t handle, int flags)
165169
{
166170
int fd;
167171

172+
_Py_BEGIN_SUPPRESS_IPH
168173
fd = _open_osfhandle(handle, flags);
174+
_Py_END_SUPPRESS_IPH
169175
if (fd == -1)
170176
PyErr_SetFromErrno(PyExc_IOError);
171177

@@ -303,7 +309,9 @@ static PyObject *
303309
msvcrt_putch_impl(PyObject *module, char char_value)
304310
/*[clinic end generated code: output=92ec9b81012d8f60 input=ec078dd10cb054d6]*/
305311
{
312+
_Py_BEGIN_SUPPRESS_IPH
306313
_putch(char_value);
314+
_Py_END_SUPPRESS_IPH
307315
Py_RETURN_NONE;
308316
}
309317

@@ -320,7 +328,9 @@ static PyObject *
320328
msvcrt_putwch_impl(PyObject *module, int unicode_char)
321329
/*[clinic end generated code: output=a3bd1a8951d28eee input=996ccd0bbcbac4c3]*/
322330
{
331+
_Py_BEGIN_SUPPRESS_IPH
323332
_putwch(unicode_char);
333+
_Py_END_SUPPRESS_IPH
324334
Py_RETURN_NONE;
325335

326336
}
@@ -342,7 +352,13 @@ static PyObject *
342352
msvcrt_ungetch_impl(PyObject *module, char char_value)
343353
/*[clinic end generated code: output=c6942a0efa119000 input=22f07ee9001bbf0f]*/
344354
{
345-
if (_ungetch(char_value) == EOF)
355+
int res;
356+
357+
_Py_BEGIN_SUPPRESS_IPH
358+
res = _ungetch(char_value);
359+
_Py_END_SUPPRESS_IPH
360+
361+
if (res == EOF)
346362
return PyErr_SetFromErrno(PyExc_IOError);
347363
Py_RETURN_NONE;
348364
}
@@ -360,7 +376,13 @@ static PyObject *
360376
msvcrt_ungetwch_impl(PyObject *module, int unicode_char)
361377
/*[clinic end generated code: output=e63af05438b8ba3d input=83ec0492be04d564]*/
362378
{
363-
if (_ungetwch(unicode_char) == WEOF)
379+
int res;
380+
381+
_Py_BEGIN_SUPPRESS_IPH
382+
res = _ungetwch(unicode_char);
383+
_Py_END_SUPPRESS_IPH
384+
385+
if (res == WEOF)
364386
return PyErr_SetFromErrno(PyExc_IOError);
365387
Py_RETURN_NONE;
366388
}
@@ -382,7 +404,13 @@ static long
382404
msvcrt_CrtSetReportFile_impl(PyObject *module, int type, int file)
383405
/*[clinic end generated code: output=df291c7fe032eb68 input=bb8f721a604fcc45]*/
384406
{
385-
return (long)_CrtSetReportFile(type, (_HFILE)file);
407+
long res;
408+
409+
_Py_BEGIN_SUPPRESS_IPH
410+
res = (long)_CrtSetReportFile(type, (_HFILE)file);
411+
_Py_END_SUPPRESS_IPH
412+
413+
return res;
386414
}
387415

388416
/*[clinic input]
@@ -403,7 +431,9 @@ msvcrt_CrtSetReportMode_impl(PyObject *module, int type, int mode)
403431
{
404432
int res;
405433

434+
_Py_BEGIN_SUPPRESS_IPH
406435
res = _CrtSetReportMode(type, mode);
436+
_Py_END_SUPPRESS_IPH
407437
if (res == -1)
408438
PyErr_SetFromErrno(PyExc_IOError);
409439
return res;
@@ -424,7 +454,13 @@ static long
424454
msvcrt_set_error_mode_impl(PyObject *module, int mode)
425455
/*[clinic end generated code: output=ac4a09040d8ac4e3 input=046fca59c0f20872]*/
426456
{
427-
return _set_error_mode(mode);
457+
long res;
458+
459+
_Py_BEGIN_SUPPRESS_IPH
460+
res = _set_error_mode(mode);
461+
_Py_END_SUPPRESS_IPH
462+
463+
return res;
428464
}
429465
#endif /* _DEBUG */
430466

@@ -443,7 +479,10 @@ msvcrt_SetErrorMode_impl(PyObject *module, unsigned int mode)
443479
{
444480
unsigned int res;
445481

482+
_Py_BEGIN_SUPPRESS_IPH
446483
res = SetErrorMode(mode);
484+
_Py_END_SUPPRESS_IPH
485+
447486
return PyLong_FromUnsignedLong(res);
448487
}
449488

0 commit comments

Comments
 (0)