summaryrefslogtreecommitdiffstats
path: root/Lib/tarfile.py
diff options
context:
space:
mode:
authorlyc8503 <me@lyc8503.site>2024-04-19 11:41:51 (GMT)
committerGitHub <noreply@github.com>2024-04-19 11:41:51 (GMT)
commit15b3555e4a47ec925c965778a415dc11f0f981fd (patch)
tree9699f4644620c576afa6d727c776b3ac09b608f9 /Lib/tarfile.py
parent3e7d990a09f0928050b2b0c85f724c2bce13fcbb (diff)
downloadcpython-15b3555e4a47ec925c965778a415dc11f0f981fd.zip
cpython-15b3555e4a47ec925c965778a415dc11f0f981fd.tar.gz
cpython-15b3555e4a47ec925c965778a415dc11f0f981fd.tar.bz2
gh-116931: Add fileobj parameter check for Tarfile.addfile (GH-117988)
Tarfile.addfile now throws an ValueError when the user passes in a non-zero size tarinfo but does not provide a fileobj, instead of writing an incomplete entry.
Diffstat (limited to 'Lib/tarfile.py')
-rwxr-xr-xLib/tarfile.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 149b1c3..78bb10c 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -2214,13 +2214,16 @@ class TarFile(object):
self.addfile(tarinfo)
def addfile(self, tarinfo, fileobj=None):
- """Add the TarInfo object `tarinfo' to the archive. If `fileobj' is
- given, it should be a binary file, and tarinfo.size bytes are read
- from it and added to the archive. You can create TarInfo objects
- directly, or by using gettarinfo().
+ """Add the TarInfo object `tarinfo' to the archive. If `tarinfo' represents
+ a non zero-size regular file, the `fileobj' argument should be a binary file,
+ and tarinfo.size bytes are read from it and added to the archive.
+ You can create TarInfo objects directly, or by using gettarinfo().
"""
self._check("awx")
+ if fileobj is None and tarinfo.isreg() and tarinfo.size != 0:
+ raise ValueError("fileobj not provided for non zero-size regular file")
+
tarinfo = copy.copy(tarinfo)
buf = tarinfo.tobuf(self.format, self.encoding, self.errors)