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: fix decompression of empty data streams #17042

Closed
wants to merge 1 commit into from

Conversation

addaleax
Copy link
Member

add4b0a made the assumption that compressed data
would never lead to an empty decompressed stream.

Fix that by explicitly checking the number of read bytes.

Fixes: #17041
Refs: #13322

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

zlib

add4b0a made the assumption that compressed data
would never lead to an empty decompressed stream.

Fix that by explicitly checking the number of read bytes.

Fixes: nodejs#17041
Refs: nodejs#13322
@addaleax addaleax added dont-land-on-v6.x zlib Issues and PRs related to the zlib subsystem. labels Nov 15, 2017
@addaleax
Copy link
Member Author

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a nit.

@@ -95,6 +95,8 @@ function zlibBufferOnEnd() {
var err;
if (this.nread >= kMaxLength) {
err = new errors.RangeError('ERR_BUFFER_TOO_LARGE');
} else if (this.nread === 0) {
buf = Buffer.alloc(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if there is any performance penalty to use Buffer.alloc(0) vs Buffer.allocUnsafe(0). However, as it's 0 I would use allocUnsafe.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alloc() has what is essentially a fast path for 0, allocUnsafe() doesn’t.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very interesting. thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, I don’t think it’s that way on purpose. :) It just happens to align nicely with how we can use basically new Uint8Array(n) for the Buffer.alloc() fast path, whereas Buffer.allocUnsafe() needs a bit of setup.

@addaleax
Copy link
Member Author

Landed in 01f853c

@addaleax addaleax closed this Nov 18, 2017
@addaleax addaleax deleted the zlib-empty branch November 18, 2017 19:41
addaleax added a commit that referenced this pull request Nov 18, 2017
add4b0a made the assumption that compressed data
would never lead to an empty decompressed stream.

Fix that by explicitly checking the number of read bytes.

PR-URL: #17042
Fixes: #17041
Refs: #13322
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MylesBorins pushed a commit that referenced this pull request Dec 12, 2017
add4b0a made the assumption that compressed data
would never lead to an empty decompressed stream.

Fix that by explicitly checking the number of read bytes.

PR-URL: #17042
Fixes: #17041
Refs: #13322
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@MylesBorins MylesBorins mentioned this pull request Dec 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
zlib Issues and PRs related to the zlib subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

zlib fails when uncompressing a previously compressed empty buffer
7 participants