Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ManuelTS opened this issue Sep 10, 2021 · 2 comments

Comments

@ManuelTS
Copy link

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.

@mscdex
Copy link
Contributor

mscdex commented Sep 10, 2021

Converting binary data to UTF-8 mangles the resulting output. If you really need to store it as a string, use something that will keep the binary representation intact like 'base64' or 'hex'(or even 'latin1' which would have the least overhead storage-wise but is not really ideal).

@ManuelTS
Copy link
Author

Thanks for your reply, works fine as you said!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants