Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 172 additions & 54 deletions Lib/imaplib.py

Large diffs are not rendered by default.

40 changes: 36 additions & 4 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,32 @@ def copyfileobj(src, dst, length=None, exception=OSError, bufsize=None):
dst.write(buf)
return

# Maximum number of bytes read in a single call when reading a member's
# extended header (a GNU long name/link or a pax header). The size of such
# a header is taken from the archive and is not trustworthy, so it is read in
# bounded chunks to avoid a huge up-front allocation when a crafted or
# truncated archive claims far more data than the file actually contains
# (gh-151497).
_EXTHEADER_READ_CHUNK = 1024 * 1024 # 1 MiB

def _safe_read(fileobj, size):
"""Read up to *size* bytes from *fileobj* in bounded chunks.

Returns the same bytes as ``fileobj.read(size)`` would (including a short
result at end of file), but limits pre-allocation, so an
oversized size field in a crafted header cannot force a huge allocation.
"""
if size <= _EXTHEADER_READ_CHUNK:
return fileobj.read(size)
chunks = []
while size > 0:
chunk = fileobj.read(min(size, _EXTHEADER_READ_CHUNK))
if not chunk:
break
chunks.append(chunk)
size -= len(chunk)
return b"".join(chunks)

def _safe_print(s):
encoding = getattr(sys.stdout, 'encoding', None)
if encoding is not None:
Expand Down Expand Up @@ -524,7 +550,9 @@ def seek(self, pos=0):
if pos - self.pos >= 0:
blocks, remainder = divmod(pos - self.pos, self.bufsize)
for i in range(blocks):
self.read(self.bufsize)
data = self.read(self.bufsize)
if not data:
break
self.read(remainder)
else:
raise StreamError("seeking backwards is not allowed")
Expand Down Expand Up @@ -1429,7 +1457,7 @@ def _proc_gnulong(self, tarfile):
"""Process the blocks that hold a GNU longname
or longlink member.
"""
buf = tarfile.fileobj.read(self._block(self.size))
buf = _safe_read(tarfile.fileobj, self._block(self.size))

# Fetch the next header and process it.
try:
Expand Down Expand Up @@ -1485,7 +1513,7 @@ def _proc_pax(self, tarfile):
POSIX.1-2008.
"""
# Read the header information.
buf = tarfile.fileobj.read(self._block(self.size))
buf = _safe_read(tarfile.fileobj, self._block(self.size))

# A pax header stores supplemental information for either
# the following file (extended) or all following files
Expand Down Expand Up @@ -2510,7 +2538,8 @@ def extract(self, member, path="", set_attrs=True, *, numeric_owner=False,
tarinfo, unfiltered = self._get_extract_tarinfo(
member, filter_function, path)
if tarinfo is not None:
self._extract_one(tarinfo, path, set_attrs, numeric_owner)
self._extract_one(tarinfo, path, set_attrs, numeric_owner,
filter_function=filter_function)

def _get_extract_tarinfo(self, member, filter_function, path):
"""Get (filtered, unfiltered) TarInfos from *member*
Expand Down Expand Up @@ -2782,6 +2811,9 @@ def makelink_with_filter(self, tarinfo, targetpath,
"makelink_with_filter: if filter_function is not None, "
+ "extraction_root must also not be None")
try:
filter_function(
unfiltered.replace(name=tarinfo.name, deep=False),
extraction_root)
filtered = filter_function(unfiltered, extraction_root)
except _FILTER_ERRORS as cause:
raise LinkFallbackError(tarinfo, unfiltered.name) from cause
Expand Down
3 changes: 2 additions & 1 deletion Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,8 @@ def __init__(self, suffix=None, prefix=None, dir=None,
@classmethod
def _rmtree(cls, name, ignore_errors=False, repeated=False):
def onexc(func, path, exc):
if isinstance(exc, PermissionError):
# On DragonFly BSD, UF_NOUNLINK removal fails with EISDIR, not EPERM.
if isinstance(exc, (PermissionError, IsADirectoryError)):
if repeated and path == name:
if ignore_errors:
return
Expand Down
Loading
Loading