Skip to content

Commit 21fae03

Browse files
committed
Issue python#29392: Prevent crash when passing invalid arguments into msvcrt module.
1 parent d3c4853 commit 21fae03

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ Library
4747
leading dots could match related hostnames again (e.g. .b.c matches a.b.c).
4848
Patch by Milan Oberkirch.
4949

50+
Windows
51+
-------
52+
53+
- Issue #29392: Prevent crash when passing invalid arguments into msvcrt module.
54+
5055
C API
5156
-----
5257

PC/msvcrtmodule.c

Lines changed: 46 additions & 12 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, Py_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

@@ -189,16 +195,11 @@ msvcrt_get_osfhandle_impl(PyObject *module, int fd)
189195
{
190196
Py_intptr_t handle = -1;
191197

192-
if (!_PyVerify_fd(fd)) {
193-
PyErr_SetFromErrno(PyExc_IOError);
194-
}
195-
else {
196198
_Py_BEGIN_SUPPRESS_IPH
197-
handle = _get_osfhandle(fd);
199+
handle = _get_osfhandle(fd);
198200
_Py_END_SUPPRESS_IPH
199-
if (handle == -1)
200-
PyErr_SetFromErrno(PyExc_IOError);
201-
}
201+
if (handle == -1)
202+
PyErr_SetFromErrno(PyExc_IOError);
202203

203204
return handle;
204205
}
@@ -308,7 +309,9 @@ static PyObject *
308309
msvcrt_putch_impl(PyObject *module, char char_value)
309310
/*[clinic end generated code: output=92ec9b81012d8f60 input=ec078dd10cb054d6]*/
310311
{
312+
_Py_BEGIN_SUPPRESS_IPH
311313
_putch(char_value);
314+
_Py_END_SUPPRESS_IPH
312315
Py_RETURN_NONE;
313316
}
314317

@@ -325,7 +328,9 @@ static PyObject *
325328
msvcrt_putwch_impl(PyObject *module, int unicode_char)
326329
/*[clinic end generated code: output=a3bd1a8951d28eee input=996ccd0bbcbac4c3]*/
327330
{
331+
_Py_BEGIN_SUPPRESS_IPH
328332
_putwch(unicode_char);
333+
_Py_END_SUPPRESS_IPH
329334
Py_RETURN_NONE;
330335

331336
}
@@ -347,7 +352,13 @@ static PyObject *
347352
msvcrt_ungetch_impl(PyObject *module, char char_value)
348353
/*[clinic end generated code: output=c6942a0efa119000 input=22f07ee9001bbf0f]*/
349354
{
350-
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)
351362
return PyErr_SetFromErrno(PyExc_IOError);
352363
Py_RETURN_NONE;
353364
}
@@ -365,7 +376,13 @@ static PyObject *
365376
msvcrt_ungetwch_impl(PyObject *module, int unicode_char)
366377
/*[clinic end generated code: output=e63af05438b8ba3d input=83ec0492be04d564]*/
367378
{
368-
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)
369386
return PyErr_SetFromErrno(PyExc_IOError);
370387
Py_RETURN_NONE;
371388
}
@@ -387,7 +404,13 @@ static long
387404
msvcrt_CrtSetReportFile_impl(PyObject *module, int type, int file)
388405
/*[clinic end generated code: output=df291c7fe032eb68 input=bb8f721a604fcc45]*/
389406
{
390-
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;
391414
}
392415

393416
/*[clinic input]
@@ -408,7 +431,9 @@ msvcrt_CrtSetReportMode_impl(PyObject *module, int type, int mode)
408431
{
409432
int res;
410433

434+
_Py_BEGIN_SUPPRESS_IPH
411435
res = _CrtSetReportMode(type, mode);
436+
_Py_END_SUPPRESS_IPH
412437
if (res == -1)
413438
PyErr_SetFromErrno(PyExc_IOError);
414439
return res;
@@ -429,7 +454,13 @@ static long
429454
msvcrt_set_error_mode_impl(PyObject *module, int mode)
430455
/*[clinic end generated code: output=ac4a09040d8ac4e3 input=046fca59c0f20872]*/
431456
{
432-
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;
433464
}
434465
#endif /* _DEBUG */
435466

@@ -448,7 +479,10 @@ msvcrt_SetErrorMode_impl(PyObject *module, unsigned int mode)
448479
{
449480
unsigned int res;
450481

482+
_Py_BEGIN_SUPPRESS_IPH
451483
res = SetErrorMode(mode);
484+
_Py_END_SUPPRESS_IPH
485+
452486
return PyLong_FromUnsignedLong(res);
453487
}
454488

0 commit comments

Comments
 (0)