Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6edec2

Browse files
author
Victor Stinner
committed
Issue #9015, #9611: FileIO.readinto(), FileIO.write() and os.write() clamp the
length to 2^31-1 on Windows.
1 parent 560f9da commit e6edec2

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ What's New in Python 3.2 Release Candidate 1
88
Core and Builtins
99
-----------------
1010

11+
- Issue #9015, #9611: FileIO.readinto(), FileIO.write() and os.write() clamp
12+
the length to 2^31-1 on Windows.
13+
1114
- Issue #8278: On Windows and with a NTFS filesystem, os.stat() and os.utime()
1215
can now handle dates after 2038.
1316

Modules/_io/fileio.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ static PyObject *
506506
fileio_readinto(fileio *self, PyObject *args)
507507
{
508508
Py_buffer pbuf;
509-
Py_ssize_t n;
509+
Py_ssize_t n, len;
510510

511511
if (self->fd < 0)
512512
return err_closed();
@@ -517,9 +517,16 @@ fileio_readinto(fileio *self, PyObject *args)
517517
return NULL;
518518

519519
if (_PyVerify_fd(self->fd)) {
520+
len = pbuf.len;
520521
Py_BEGIN_ALLOW_THREADS
521522
errno = 0;
522-
n = read(self->fd, pbuf.buf, pbuf.len);
523+
#if defined(MS_WIN64) || defined(MS_WINDOWS)
524+
if (len > INT_MAX)
525+
len = INT_MAX;
526+
n = read(self->fd, pbuf.buf, (int)len);
527+
#else
528+
n = read(self->fd, pbuf.buf, (size_t)len);
529+
#endif
523530
Py_END_ALLOW_THREADS
524531
} else
525532
n = -1;
@@ -685,7 +692,7 @@ static PyObject *
685692
fileio_write(fileio *self, PyObject *args)
686693
{
687694
Py_buffer pbuf;
688-
Py_ssize_t n;
695+
Py_ssize_t n, len;
689696

690697
if (self->fd < 0)
691698
return err_closed();
@@ -698,7 +705,14 @@ fileio_write(fileio *self, PyObject *args)
698705
if (_PyVerify_fd(self->fd)) {
699706
Py_BEGIN_ALLOW_THREADS
700707
errno = 0;
701-
n = write(self->fd, pbuf.buf, pbuf.len);
708+
len = pbuf.len;
709+
#if defined(MS_WIN64) || defined(MS_WINDOWS)
710+
if (len > INT_MAX)
711+
len = INT_MAX;
712+
n = write(self->fd, pbuf.buf, (int)len);
713+
#else
714+
n = write(self->fd, pbuf.buf, (size_t)len);
715+
#endif
702716
Py_END_ALLOW_THREADS
703717
} else
704718
n = -1;

Modules/posixmodule.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5696,16 +5696,23 @@ posix_write(PyObject *self, PyObject *args)
56965696
{
56975697
Py_buffer pbuf;
56985698
int fd;
5699-
Py_ssize_t size;
5699+
Py_ssize_t size, len;
57005700

57015701
if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
57025702
return NULL;
57035703
if (!_PyVerify_fd(fd)) {
57045704
PyBuffer_Release(&pbuf);
57055705
return posix_error();
57065706
}
5707+
len = pbuf.len;
57075708
Py_BEGIN_ALLOW_THREADS
5708-
size = write(fd, pbuf.buf, (size_t)pbuf.len);
5709+
#if defined(MS_WIN64) || defined(MS_WINDOWS)
5710+
if (len > INT_MAX)
5711+
len = INT_MAX;
5712+
size = write(fd, pbuf.buf, (int)len);
5713+
#else
5714+
size = write(fd, pbuf.buf, (size_t)len);
5715+
#endif
57095716
Py_END_ALLOW_THREADS
57105717
PyBuffer_Release(&pbuf);
57115718
if (size < 0)

0 commit comments

Comments
 (0)