Skip to content

Commit 22c8a43

Browse files
authored
Merge pull request #21 from ActiveState/PB-4141
PB-4252 backport PC\layout to 2.7 branch
2 parents e49af37 + 809d473 commit 22c8a43

25 files changed

+1470
-203
lines changed

Include/fileobject.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,6 @@ size_t Py_UniversalNewlineFread(char *, size_t, FILE *, PyObject *);
7070
*/
7171
int _PyFile_SanitizeMode(char *mode);
7272

73-
#if defined _MSC_VER && _MSC_VER >= 1400
74-
/* A routine to check if a file descriptor is valid on Windows. Returns 0
75-
* and sets errno to EBADF if it isn't. This is to avoid Assertions
76-
* from various functions in the Windows CRT beginning with
77-
* Visual Studio 2005
78-
*/
79-
int _PyVerify_fd(int fd);
80-
#elif defined _MSC_VER && _MSC_VER >= 1200
81-
/* fdopen doesn't set errno EBADF and crashes for large fd on debug build */
82-
#define _PyVerify_fd(fd) (_get_osfhandle(fd) >= 0)
83-
#else
84-
#define _PyVerify_fd(A) (1) /* dummy */
85-
#endif
86-
8773
/* A routine to check if a file descriptor can be select()-ed. */
8874
#ifdef HAVE_SELECT
8975
#define _PyIsSelectable_fd(FD) (((FD) >= 0) && ((FD) < FD_SETSIZE))

Include/pyport.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,4 +947,24 @@ typedef struct fd_set {
947947
#define Py_ULL(x) Py_LL(x##U)
948948
#endif
949949

950+
#ifdef Py_BUILD_CORE
951+
/*
952+
* Macros to protect CRT calls against instant termination when passed an
953+
* invalid parameter (issue23524).
954+
*/
955+
#if defined _MSC_VER && _MSC_VER >= 1900
956+
957+
extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
958+
#define _Py_BEGIN_SUPPRESS_IPH { _invalid_parameter_handler _Py_old_handler = \
959+
_set_thread_local_invalid_parameter_handler(_Py_silent_invalid_parameter_handler);
960+
#define _Py_END_SUPPRESS_IPH _set_thread_local_invalid_parameter_handler(_Py_old_handler); }
961+
962+
#else
963+
964+
#define _Py_BEGIN_SUPPRESS_IPH
965+
#define _Py_END_SUPPRESS_IPH
966+
967+
#endif /* _MSC_VER >= 1900 */
968+
#endif /* Py_BUILD_CORE */
969+
950970
#endif /* Py_PYPORT_H */

Modules/_io/fileio.c

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,13 @@ internal_close(fileio *self)
8282
int fd = self->fd;
8383
self->fd = -1;
8484
/* fd is accessible and someone else may have closed it */
85-
if (_PyVerify_fd(fd)) {
86-
Py_BEGIN_ALLOW_THREADS
87-
err = close(fd);
88-
if (err < 0)
89-
save_errno = errno;
90-
Py_END_ALLOW_THREADS
91-
} else {
85+
Py_BEGIN_ALLOW_THREADS
86+
_Py_BEGIN_SUPPRESS_IPH
87+
err = close(fd);
88+
if (err < 0)
9289
save_errno = errno;
93-
err = -1;
94-
}
90+
_Py_END_SUPPRESS_IPH
91+
Py_END_ALLOW_THREADS
9592
}
9693
if (err < 0) {
9794
errno = save_errno;
@@ -172,12 +169,10 @@ check_fd(int fd)
172169
PyObject *exc;
173170
char *msg;
174171

175-
if (!_PyVerify_fd(fd)) {
176-
goto badfd;
177-
}
178-
179172
Py_BEGIN_ALLOW_THREADS
173+
_Py_BEGIN_SUPPRESS_IPH
180174
res = fstat(fd, &buf);
175+
_Py_END_SUPPRESS_IPH
181176
Py_END_ALLOW_THREADS
182177

183178
if (res < 0 && errno == EBADF) {
@@ -511,20 +506,19 @@ fileio_readinto(fileio *self, PyObject *args)
511506
if (!PyArg_ParseTuple(args, "w*", &pbuf))
512507
return NULL;
513508

514-
if (_PyVerify_fd(self->fd)) {
515-
len = pbuf.len;
516-
Py_BEGIN_ALLOW_THREADS
517-
errno = 0;
509+
len = pbuf.len;
510+
Py_BEGIN_ALLOW_THREADS
511+
_Py_BEGIN_SUPPRESS_IPH
512+
errno = 0;
518513
#if defined(MS_WIN64) || defined(MS_WINDOWS)
519-
if (len > INT_MAX)
520-
len = INT_MAX;
521-
n = read(self->fd, pbuf.buf, (int)len);
514+
if (len > INT_MAX)
515+
len = INT_MAX;
516+
n = read(self->fd, pbuf.buf, (int)len);
522517
#else
523-
n = read(self->fd, pbuf.buf, len);
518+
n = read(self->fd, pbuf.buf, len);
524519
#endif
525-
Py_END_ALLOW_THREADS
526-
} else
527-
n = -1;
520+
_Py_END_SUPPRESS_IPH
521+
Py_END_ALLOW_THREADS
528522
PyBuffer_Release(&pbuf);
529523
if (n < 0) {
530524
if (errno == EAGAIN)
@@ -580,8 +574,6 @@ fileio_readall(fileio *self)
580574

581575
if (self->fd < 0)
582576
return err_closed();
583-
if (!_PyVerify_fd(self->fd))
584-
return PyErr_SetFromErrno(PyExc_IOError);
585577

586578
result = PyBytes_FromStringAndSize(NULL, SMALLCHUNK);
587579
if (result == NULL)
@@ -602,6 +594,7 @@ fileio_readall(fileio *self)
602594
return NULL; /* result has been freed */
603595
}
604596
Py_BEGIN_ALLOW_THREADS
597+
_Py_BEGIN_SUPPRESS_IPH
605598
errno = 0;
606599
n = newsize - total;
607600
#if defined(MS_WIN64) || defined(MS_WINDOWS)
@@ -615,6 +608,7 @@ fileio_readall(fileio *self)
615608
PyBytes_AS_STRING(result) + total,
616609
n);
617610
#endif
611+
_Py_END_SUPPRESS_IPH
618612
Py_END_ALLOW_THREADS
619613
if (n == 0)
620614
break;
@@ -677,17 +671,16 @@ fileio_read(fileio *self, PyObject *args)
677671
return NULL;
678672
ptr = PyBytes_AS_STRING(bytes);
679673

680-
if (_PyVerify_fd(self->fd)) {
681-
Py_BEGIN_ALLOW_THREADS
682-
errno = 0;
674+
Py_BEGIN_ALLOW_THREADS
675+
_Py_BEGIN_SUPPRESS_IPH
676+
errno = 0;
683677
#if defined(MS_WIN64) || defined(MS_WINDOWS)
684-
n = read(self->fd, ptr, (int)size);
678+
n = read(self->fd, ptr, (int)size);
685679
#else
686-
n = read(self->fd, ptr, size);
680+
n = read(self->fd, ptr, size);
687681
#endif
688-
Py_END_ALLOW_THREADS
689-
} else
690-
n = -1;
682+
_Py_END_SUPPRESS_IPH
683+
Py_END_ALLOW_THREADS
691684

692685
if (n < 0) {
693686
Py_DECREF(bytes);
@@ -727,20 +720,19 @@ fileio_write(fileio *self, PyObject *args)
727720
return NULL;
728721
}
729722

730-
if (_PyVerify_fd(self->fd)) {
731-
Py_BEGIN_ALLOW_THREADS
732-
errno = 0;
733-
len = pbuf.len;
723+
Py_BEGIN_ALLOW_THREADS
724+
_Py_BEGIN_SUPPRESS_IPH
725+
errno = 0;
726+
len = pbuf.len;
734727
#if defined(MS_WIN64) || defined(MS_WINDOWS)
735-
if (len > INT_MAX)
736-
len = INT_MAX;
737-
n = write(self->fd, pbuf.buf, (int)len);
728+
if (len > INT_MAX)
729+
len = INT_MAX;
730+
n = write(self->fd, pbuf.buf, (int)len);
738731
#else
739-
n = write(self->fd, pbuf.buf, len);
732+
n = write(self->fd, pbuf.buf, len);
740733
#endif
741-
Py_END_ALLOW_THREADS
742-
} else
743-
n = -1;
734+
_Py_END_SUPPRESS_IPH
735+
Py_END_ALLOW_THREADS
744736

745737
PyBuffer_Release(&pbuf);
746738

@@ -793,16 +785,15 @@ portable_lseek(int fd, PyObject *posobj, int whence)
793785
return NULL;
794786
}
795787

796-
if (_PyVerify_fd(fd)) {
797-
Py_BEGIN_ALLOW_THREADS
788+
Py_BEGIN_ALLOW_THREADS
789+
_Py_BEGIN_SUPPRESS_IPH
798790
#if defined(MS_WIN64) || defined(MS_WINDOWS)
799-
res = _lseeki64(fd, pos, whence);
791+
res = _lseeki64(fd, pos, whence);
800792
#else
801-
res = lseek(fd, pos, whence);
793+
res = lseek(fd, pos, whence);
802794
#endif
803-
Py_END_ALLOW_THREADS
804-
} else
805-
res = -1;
795+
_Py_END_SUPPRESS_IPH
796+
Py_END_ALLOW_THREADS
806797
if (res < 0)
807798
return PyErr_SetFromErrno(PyExc_IOError);
808799

Modules/mmapmodule.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,11 +1301,9 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
13011301
*/
13021302
if (fileno != -1 && fileno != 0) {
13031303
/* Ensure that fileno is within the CRT's valid range */
1304-
if (_PyVerify_fd(fileno) == 0) {
1305-
PyErr_SetFromErrno(mmap_module_error);
1306-
return NULL;
1307-
}
1304+
_Py_BEGIN_SUPPRESS_IPH
13081305
fh = (HANDLE)_get_osfhandle(fileno);
1306+
_Py_END_SUPPRESS_IPH
13091307
if (fh==(HANDLE)-1) {
13101308
PyErr_SetFromErrno(mmap_module_error);
13111309
return NULL;

0 commit comments

Comments
 (0)