Skip to content

tarfiles: AttributeError: '_Stream' object has no attribute 'exception' while trying to open tgz file #107396

Closed

Description

@balmeida-nokia

Bug report

If a tar file appears to be a tar.gz file, it can fail in this block of tarfile.py

        while c < size:
            # Skip underlying buffer to avoid unaligned double buffering.
            if self.buf:
                buf = self.buf
                self.buf = b""
            else:
                buf = self.fileobj.read(self.bufsize)
                if not buf:
                    break
            try:
                buf = self.cmp.decompress(buf)
            except self.exception as e:
                raise ReadError("invalid compressed data") from e
            t.append(buf)
            c += len(buf)

at:

buf = self.cmp.decompress(buf)

before self.exception is set.
Because around:

            if comptype == "gz":
                try:
                    import zlib
                except ImportError:
                    raise CompressionError("zlib module is not available") from None
                self.zlib = zlib
                self.crc = zlib.crc32(b"")
                if mode == "r":
                    self._init_read_gz()
                    self.exception = zlib.error
                else:
                    self._init_write_gz()

This: self._init_read_gz()
is called before self.exception = zlib.error

Stack trace:

_read, tarfile.py:548
read, tarfile.py:526
_init_read_gz, tarfile.py:491
__init__, tarfile.py:375
open, tarfile.py:1827
<module>, test.py:2

In my case, buf = self.cmp.decompress(buf) failed to execute but that's a separate issue that may or may not be a bug

Your environment

  • CPython versions tested on: 3.11.4
  • Operating system and architecture: Windows 10 21H2

Linked PRs

Activity

  1. added
    type-bug
    on Jul 28, 2023
  2. sunmy2019 commented on Jul 29, 2023

    @sunmy2019
    Member

    Looks reasonable. Will you open a PR?

  3. added
    stdlib
    3.11
    3.12
    3.13
    on Jul 29, 2023
  4. balmeida-nokia commented on Jul 31, 2023

    @balmeida-nokia
    ContributorAuthor

    Sure

  5. added 5 commits that reference this issue on Jul 31, 2023
  6. encukou commented on Aug 10, 2023

    @encukou
    Member

    Hello,
    How does this issue manifest? exception appears like an write-only attribute of an internal class.
    I'd like to add a test for the PR, but can't think of a reasonable one. Do you have a use case we currently don't have tests for?

  7. balmeida-nokia commented on Aug 10, 2023

    @balmeida-nokia
    ContributorAuthor

    @encukou Please see: #107398

    Maybe that one will explain better the sequence where this failure can happen.

    My interpretation is that gzip supports backwards skipping and it appears to be rarely used. If I open a tgz with r|* (which, by design, doesn't support backwards skipping) and the gzip stream requests a backwards skip, an exception is raised. At the except statement it has except self.exception which is were the attribute is read, which means the attribute is actually read-write.

  8. encukou commented on Aug 10, 2023

    @encukou
    Member

    Oh, I see!
    Here's a reproducer:

    with open('hm.tar.gz', 'rb') as f:
        f = io.BytesIO(
            b'\x1f\x8b'  # header
            + b'\x08'  # compression method
            + b'\x04'  # flags
            + b'\0\0\0\0\0\0'  # padding?
            + b'\0\x01'  # size
            + bytes(5)  # corrupt data (zeros)
        )
        tarfile.open(fileobj=f, mode='r|gz')

    If you can add it as a test using with self.assertRaises(tarfile.ReadError): to Lib/test/test_tarfile.py, it'd speed things up. If not I'll get to it later.

  9. balmeida-nokia commented on Aug 10, 2023

    @balmeida-nokia
    ContributorAuthor

    OK. I will.

  10. added a commit that references this issue on Aug 18, 2023
  11. balmeida-nokia commented on Aug 18, 2023

    @balmeida-nokia
    ContributorAuthor

    @encukou Made a commit with the tests. I'm unsure if I placed the new class in the correct placement but it seemed to be one of the best places to place it. I tried to figure out if a new class should be done and seemed to be a reasonable choice.
    #107485

    (sorry for the delay. Ty for waiting)

  12. added a commit that references this issue on Aug 21, 2023
  13. added a commit that references this issue on Aug 21, 2023
  14. added 2 commits that reference this issue on Aug 21, 2023
  15. added a commit that references this issue on Aug 21, 2023
  16. added a commit that references this issue on Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.113.123.13stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions