Skip to content

zlib.decompressobj fails after documented-safe zdict mutation with a partial initial header #158773

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Python Library Reference, zlib.decompressobj(): “If zdict is a mutable object (such as a bytearray), you must not modify its contents between the call to decompressobj() and the first call to the decompressor’s decompress() method.”

Expected: Subsequent decompress() calls and flush() recover b'\x00\x00\x00\x00' using the original dictionary contents.

Actual: The subsequent decompress() call raises zlib.error: Error -3 while setting zdict: invalid input data.

import zlib

D = bytearray(b"\0")
P = b"\0" * 4
case = dict(dictionary=D[:], payload=P, mutation="replace each byte b with b ^ 255",
            first_chunk_length=1, wbits=15)
try:
    snapshot = bytes(D)
    c = zlib.compressobj(wbits=15, zdict=snapshot)
    stream = c.compress(P) + c.flush()
    reference = zlib.decompressobj(wbits=15, zdict=snapshot)
    expected = reference.decompress(stream) + reference.flush()
    if not D or expected != P or not reference.eof:
        print("REFUTATION REJECTED:", "input failed immutable-dictionary validation")
    else:
        try:
            d = zlib.decompressobj(wbits=15, zdict=D)
            actual = d.decompress(stream[:1])
            # Mutation occurs after the first decompress() call, as documented.
            D[:] = bytes(b ^ 255 for b in D)
            actual += d.decompress(stream[1:]) + d.flush()
        except Exception as e:
            actual = ("EXCEPTION", type(e).__name__, str(e))
        if actual != expected:
            print("REFUTATION CONFIRMED:", case, "actual:", actual, "expected:", expected)
        else:
            print("REFUTATION REJECTED:", "documented expectation was recovered")
except Exception as e:
    print("REFUTATION REJECTED:", "validation/setup failed:", type(e).__name__, str(e))

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library zlib:

REFUTATION CONFIRMED: {'dictionary': bytearray(b'\x00'), 'payload': b'\x00\x00\x00\x00', 'mutation': 'replace each byte b with b ^ 255', 'first_chunk_length': 1, 'wbits': 15} actual: ('EXCEPTION', 'error', 'Error -3 while setting zdict: invalid input data') expected: b'\x00\x00\x00\x00'

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/zlib-20261003-065622-c2

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions