Skip to content

Commit 94e1650

Browse files
authored
bpo-38319: Fix shutil._fastcopy_sendfile(): set sendfile() max block size (GH-16491)
1 parent cf57cab commit 94e1650

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

Lib/shutil.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,13 @@ def _fastcopy_sendfile(fsrc, fdst):
135135
# should not make any difference, also in case the file content
136136
# changes while being copied.
137137
try:
138-
blocksize = max(os.fstat(infd).st_size, 2 ** 23) # min 8MB
139-
except Exception:
140-
blocksize = 2 ** 27 # 128MB
138+
blocksize = max(os.fstat(infd).st_size, 2 ** 23) # min 8MiB
139+
except OSError:
140+
blocksize = 2 ** 27 # 128MiB
141+
# On 32-bit architectures truncate to 1GiB to avoid OverflowError,
142+
# see bpo-38319.
143+
if sys.maxsize < 2 ** 32:
144+
blocksize = min(blocksize, 2 ** 30)
141145

142146
offset = 0
143147
while True:

Lib/socket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ def _sendfile_use_sendfile(self, file, offset=0, count=None):
356356
raise _GiveupOnSendfile(err) # not a regular file
357357
if not fsize:
358358
return 0 # empty file
359-
blocksize = fsize if not count else count
360-
359+
# Truncate to 1GiB to avoid OverflowError, see bpo-38319.
360+
blocksize = min(count or fsize, 2 ** 30)
361361
timeout = self.gettimeout()
362362
if timeout == 0:
363363
raise ValueError("non-blocking sockets are not supported")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sendfile() used in socket and shutil modules was raising OverflowError for
2+
files >= 2GiB on 32-bit architectures. (patch by Giampaolo Rodola)

0 commit comments

Comments
 (0)