Skip to content

zipfile library will raise uncaught oserror when reading length incorrect zip file #91087

Description

@ultimalium
mannequin
BPO 46931
Files
  • damaged.zip: use notepad to open and deleted some contents of the zip file
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2022-03-05.17:37:33.918>
    labels = ['type-bug', 'library', '3.10']
    title = 'zipfile library will raise uncaught oserror when reading length incorrect zip file'
    updated_at = <Date 2022-03-05.17:37:33.918>
    user = 'https://bugs.python.org/ultimalium'

    bugs.python.org fields:

    activity = <Date 2022-03-05.17:37:33.918>
    actor = 'ultimalium'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2022-03-05.17:37:33.918>
    creator = 'ultimalium'
    dependencies = []
    files = ['50658']
    hgrepos = []
    issue_num = 46931
    keywords = []
    message_count = 1.0
    messages = ['414590']
    nosy_count = 1.0
    nosy_names = ['ultimalium']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46931'
    versions = ['Python 3.10']

    Linked PRs

    Activity

    1. ultimalium commented on Mar 5, 2022

      ultimaliummannequin
      MannequinAuthor

      I intentionally made some corrupted zip archive files for testing.
      If some contents were removed from the archive instead of changing the bits. when trying to read such files, the zipfile will raise an uncaught OSError, instead of a badzipfile error as expected.

      os is windows 10 x64 not sure if this also happens on the UNIX system or it's intended to be happen.

      code:

      import zipfile
      with zipfile.ZipFile(r'damaged.zip') as dmg:
          dmg.testzip()

      result:
      OSError
      [Errno 22] Invalid argument
      File "test.py", line 20, in <module>
      file = dmg.testzip()

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Mar 5, 2022
    3. transferred this issue fromon Apr 10, 2022
    4. MrSeemsGood commented on Oct 25, 2023

      @MrSeemsGood

      has this been fixed in newer versions of python? I'm getting the same error while trying to unpack a .bz2 archive on python 3.8.5, while python 3.11's zipfile returns BadZipFile exception, as expected.

    5. serhiy-storchaka commented on May 18, 2026

      @serhiy-storchaka
      Member

      Well, testzip() only catches BadZipFile. It does not catch the compression errors and OSError. This is not clearly documented, and would perhaps be worth to catch the compression errors. This OSError was caused by seek() with negative argument, which is a zipfile integrity error. Should it catch all OSErrors or only handle this particular case (offset can be checked before calling seek())?

      You can easy reimplement this method in your code with larger flexibility. But it seems that some people found it useful.

    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.10 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions