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

Skip to content

Commit 178d1c0

Browse files
bpo-24658: Fix read/write greater than 2 GiB on macOS (GH-1705)
On macOS, fix reading from and writing into a file with a size larger than 2 GiB. (cherry picked from commit 74a8b6e) Co-authored-by: Stéphane Wirtel <[email protected]>
1 parent 35ae99d commit 178d1c0

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
@@ -60,6 +60,19 @@ PyAPI_FUNC(int) _Py_EncodeLocaleEx(
6060
#ifndef Py_LIMITED_API
6161
PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
6262

63+
#if defined(MS_WINDOWS) || defined(__APPLE__)
64+
/* On Windows, the count parameter of read() is an int (bpo-9015, bpo-9611).
65+
On macOS 10.13, read() and write() with more than INT_MAX bytes
66+
fail with EINVAL (bpo-24658). */
67+
# define _PY_READ_MAX INT_MAX
68+
# define _PY_WRITE_MAX INT_MAX
69+
#else
70+
/* write() should truncate the input to PY_SSIZE_T_MAX bytes,
71+
but it's safer to do it ourself to have a portable behaviour */
72+
# define _PY_READ_MAX PY_SSIZE_T_MAX
73+
# define _PY_WRITE_MAX PY_SSIZE_T_MAX
74+
#endif
75+
6376
#ifdef MS_WINDOWS
6477
struct _Py_stat_struct {
6578
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
@@ -1363,18 +1363,9 @@ _Py_read(int fd, void *buf, size_t count)
13631363
* handler raised an exception. */
13641364
assert(!PyErr_Occurred());
13651365

1366-
#ifdef MS_WINDOWS
1367-
if (count > INT_MAX) {
1368-
/* On Windows, the count parameter of read() is an int */
1369-
count = INT_MAX;
1370-
}
1371-
#else
1372-
if (count > PY_SSIZE_T_MAX) {
1373-
/* if count is greater than PY_SSIZE_T_MAX,
1374-
* read() result is undefined */
1375-
count = PY_SSIZE_T_MAX;
1366+
if (count > _PY_READ_MAX) {
1367+
count = _PY_READ_MAX;
13761368
}
1377-
#endif
13781369

13791370
_Py_BEGIN_SUPPRESS_IPH
13801371
do {
@@ -1425,15 +1416,10 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
14251416
depending on heap usage). */
14261417
count = 32767;
14271418
}
1428-
else if (count > INT_MAX)
1429-
count = INT_MAX;
1430-
#else
1431-
if (count > PY_SSIZE_T_MAX) {
1432-
/* write() should truncate count to PY_SSIZE_T_MAX, but it's safer
1433-
* to do it ourself to have a portable behaviour. */
1434-
count = PY_SSIZE_T_MAX;
1435-
}
14361419
#endif
1420+
if (count > _PY_WRITE_MAX) {
1421+
count = _PY_WRITE_MAX;
1422+
}
14371423

14381424
if (gil_held) {
14391425
do {

0 commit comments

Comments
 (0)