Skip to content

zlib: toString() of the returned Buffer of gsync() causes "incorrect header check" #40062

@ManuelTS

Description

@ManuelTS

Version

14.7.5

Platform

Linux dn 5.11.0-34-generic #36-Ubuntu SMP x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

/Example zlib.gzipSync().toString() causes an Error: incorrect header check./
const zlib = require("zlib");

const zipHell = 'hello';
const encoding = 'utf8';
const zippedString = zlib.gzipSync(zipHell).toString(encoding); // Wrong causes error later

console.log('Zipped:');
console.log(zippedString);

const zippedBuffer = zlib.gzipSync(zipHell); // Works as expected

const unzippedBuffer = zlib.gunzipSync(Buffer.from(zippedBuffer, encoding)).toString(encoding); // No error
console.log('Unzipped buffer:');
console.log(unzippedBuffer);

const unzipped = zlib.gunzipSync(Buffer.from(zippedString, encoding)).toString(encoding); // Error due to toString('utf8')
console.log('Unzipped string:');
console.log(unzipped);

How often does it reproduce? Is there a required condition?

Everytime, just run the above code

What is the expected behavior?

To return the previously zipped string

What do you see instead?

zlib.js:424
throw error;
^

Error: incorrect header check
at Zlib.zlibOnError [as onerror] (zlib.js:170:17)
at processChunkSync (zlib.js:416:12)
at zlibBufferSync (zlib.js:156:12)
at Object.syncBufferWrapper [as gunzipSync] (zlib.js:755:14)
at Object. (HelloWorld.js:17:23)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10) {
errno: -3,
code: 'Z_DATA_ERROR'

Additional information

I verified this on my private laptop and work laptop, on

https://onecompiler.com/nodejs/3xb2q52ja

you will see the same error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions