Skip to content

Commit 74a8b6e

Browse files
matrixisevstinner
authored andcommitted
bpo-24658: Fix read/write greater than 2 GiB on macOS (pythonGH-1705)
On macOS, fix reading from and writing into a file with a size larger than 2 GiB.
1 parent 0f11a88 commit 74a8b6e

File tree

5 files changed

+33
-26
lines changed

5 files changed

+33
-26
lines changed

Include/fileutils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ PyAPI_FUNC(int) _Py_EncodeLocaleEx(
8181
#ifndef Py_LIMITED_API
8282
PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
8383

84+
#if defined(MS_WINDOWS) || defined(__APPLE__)
85+
/* On Windows, the count parameter of read() is an int (bpo-9015, bpo-9611).
86+
On macOS 10.13, read() and write() with more than INT_MAX bytes
87+
fail with EINVAL (bpo-24658). */
88+
# define _PY_READ_MAX INT_MAX
89+
# define _PY_WRITE_MAX INT_MAX
90+
#else
91+
/* write() should truncate the input to PY_SSIZE_T_MAX bytes,
92+
but it's safer to do it ourself to have a portable behaviour */
93+
# define _PY_READ_MAX PY_SSIZE_T_MAX
94+
# define _PY_WRITE_MAX PY_SSIZE_T_MAX
95+
#endif
96+
8497
#ifdef MS_WINDOWS
8598
struct _Py_stat_struct {
8699
unsigned long st_dev;

Lib/test/test_largefile.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import stat
66
import sys
77
import unittest
8-
from test.support import TESTFN, requires, unlink
8+
from test.support import TESTFN, requires, unlink, bigmemtest
99
import io # C implementation of io
1010
import _pyio as pyio # Python implementation of io
1111

1212
# size of file to create (>2 GiB; 2 GiB == 2,147,483,648 bytes)
13-
size = 2500000000
13+
size = 2_500_000_000
1414

1515
class LargeFileTest:
1616
"""Test that each file function works as expected for large
@@ -45,6 +45,15 @@ def tearDownClass(cls):
4545
raise cls.failureException('File was not truncated by opening '
4646
'with mode "wb"')
4747

48+
# _pyio.FileIO.readall() uses a temporary bytearray then casted to bytes,
49+
# so memuse=2 is needed
50+
@bigmemtest(size=size, memuse=2, dry_run=False)
51+
def test_large_read(self, _size):
52+
# bpo-24658: Test that a read greater than 2GB does not fail.
53+
with self.open(TESTFN, "rb") as f:
54+
self.assertEqual(len(f.read()), size + 1)
55+
self.assertEqual(f.tell(), size + 1)
56+
4857
def test_osstat(self):
4958
self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1)
5059

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
On macOS, fix reading from and writing into a file with a size larger than 2 GiB.

Modules/_io/fileio.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -791,11 +791,9 @@ _io_FileIO_read_impl(fileio *self, Py_ssize_t size)
791791
if (size < 0)
792792
return _io_FileIO_readall_impl(self);
793793

794-
#ifdef MS_WINDOWS
795-
/* On Windows, the count parameter of read() is an int */
796-
if (size > INT_MAX)
797-
size = INT_MAX;
798-
#endif
794+
if (size > _PY_READ_MAX) {
795+
size = _PY_READ_MAX;
796+
}
799797

800798
bytes = PyBytes_FromStringAndSize(NULL, size);
801799
if (bytes == NULL)

Python/fileutils.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,18 +1471,9 @@ _Py_read(int fd, void *buf, size_t count)
14711471
* handler raised an exception. */
14721472
assert(!PyErr_Occurred());
14731473

1474-
#ifdef MS_WINDOWS
1475-
if (count > INT_MAX) {
1476-
/* On Windows, the count parameter of read() is an int */
1477-
count = INT_MAX;
1478-
}
1479-
#else
1480-
if (count > PY_SSIZE_T_MAX) {
1481-
/* if count is greater than PY_SSIZE_T_MAX,
1482-
* read() result is undefined */
1483-
count = PY_SSIZE_T_MAX;
1474+
if (count > _PY_READ_MAX) {
1475+
count = _PY_READ_MAX;
14841476
}
1485-
#endif
14861477

14871478
_Py_BEGIN_SUPPRESS_IPH
14881479
do {
@@ -1533,15 +1524,10 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
15331524
depending on heap usage). */
15341525
count = 32767;
15351526
}
1536-
else if (count > INT_MAX)
1537-
count = INT_MAX;
1538-
#else
1539-
if (count > PY_SSIZE_T_MAX) {
1540-
/* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1541-
* to do it ourself to have a portable behaviour. */
1542-
count = PY_SSIZE_T_MAX;
1543-
}
15441527
#endif
1528+
if (count > _PY_WRITE_MAX) {
1529+
count = _PY_WRITE_MAX;
1530+
}
15451531

15461532
if (gil_held) {
15471533
do {

0 commit comments

Comments
 (0)