nodejs / nodejs/node

zlib: createUnzip().reset() does not restore auto-detection state

Open
#64,619 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

latest main branch

Platform
7.1.3-arch1-3
Subsystem

zlib

What steps will reproduce the bug?
'use strict';

const zlib = require('node:zlib');

const output = [];
const unzip = zlib.createUnzip();

unzip.on('data', (chunk) => output.push(chunk));
unzip.on('error', console.error);
unzip.on('end', () => {
  console.log(Buffer.concat(output).toString());
});

unzip.write(Buffer.from([0x1f]), () => {
  unzip.reset();
  unzip.end(zlib.gzipSync('hello'));
});
How often does it reproduce? Is there a required condition?

Every

What is the expected behavior? Why is that the expected behavior?

The reset stream should behave like a newly created Unzip stream and output:

hello

This is consistent with the documented behavior that zlib.reset() resets the compressor/decompressor to factory defaults.

What do you see instead?

The stream emits Z_DATA_ERROR.

After reading 0x1f, gzip_id_bytes_read_ is set to 1, while mode_ remains UNZIP. The reset implementation does not handle UNZIP, so the underlying inflater and the gzip magic-byte counter are not reset.

The same state leak also occurs after auto-detection completes: mode_ remains INFLATE or GUNZIP instead of returning to UNZIP, so the next stream does not start with fresh format detection.

Additional information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the createUnzip() and reset() entry points and run the provided reproduction, confirming that resetting after the partial 0x1f input produces Z_DATA_ERROR. Trace the reset behavior for auto-detection and verify that the stream can then process gzipSync('hello') and emit 'hello'.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.