diff options
author | Georg Brandl <georg@python.org> | 2006-12-19 22:06:46 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2006-12-19 22:06:46 (GMT) |
commit | ebbeed781d923494f782f0750e76ad4aac8e29f5 (patch) | |
tree | dafb83ba387ff94a63cbd9b0493bcfd398f5bf1a /Lib | |
parent | 8183c635bc9906b2344c0b369955d478dce15088 (diff) | |
download | cpython-ebbeed781d923494f782f0750e76ad4aac8e29f5.zip cpython-ebbeed781d923494f782f0750e76ad4aac8e29f5.tar.gz cpython-ebbeed781d923494f782f0750e76ad4aac8e29f5.tar.bz2 |
Patch #1484695: The tarfile module now raises a HeaderError exception
if a buffer given to frombuf() is invalid.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/tarfile.py | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 1b8f140..00789f3 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -280,6 +280,9 @@ class CompressionError(TarError): class StreamError(TarError): """Exception for unsupported operations on stream-like TarFiles.""" pass +class HeaderError(TarError): + """Exception for invalid headers.""" + pass #--------------------------- # internal stream interface @@ -819,9 +822,17 @@ class TarInfo(object): """Construct a TarInfo object from a 512 byte string buffer. """ if len(buf) != BLOCKSIZE: - raise ValueError("truncated header") + raise HeaderError("truncated header") if buf.count(NUL) == BLOCKSIZE: - raise ValueError("empty header") + raise HeaderError("empty header") + + try: + chksum = nti(buf[148:156]) + except ValueError: + raise HeaderError("invalid header") + + if chksum not in calc_chksums(buf): + raise HeaderError("bad checksum") tarinfo = cls() tarinfo.buf = buf @@ -831,7 +842,7 @@ class TarInfo(object): tarinfo.gid = nti(buf[116:124]) tarinfo.size = nti(buf[124:136]) tarinfo.mtime = nti(buf[136:148]) - tarinfo.chksum = nti(buf[148:156]) + tarinfo.chksum = chksum tarinfo.type = buf[156:157] tarinfo.linkname = buf[157:257].rstrip(NUL) tarinfo.uname = buf[265:297].rstrip(NUL) @@ -843,8 +854,6 @@ class TarInfo(object): if prefix and not tarinfo.issparse(): tarinfo.name = prefix + "/" + tarinfo.name - if tarinfo.chksum not in calc_chksums(buf): - raise ValueError("invalid header") return tarinfo def tobuf(self, posix=False): @@ -1793,16 +1802,14 @@ class TarFile(object): tarinfo = self.proc_member(tarinfo) - except ValueError, e: + except HeaderError, e: if self.ignore_zeros: - self._dbg(2, "0x%X: empty or invalid block: %s" % - (self.offset, e)) + self._dbg(2, "0x%X: %s" % (self.offset, e)) self.offset += BLOCKSIZE continue else: if self.offset == 0: - raise ReadError("empty, unreadable or compressed " - "file: %s" % e) + raise ReadError(str(e)) return None break |