Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
zlib: fix decompression of empty data streams
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
  • Loading branch information
addaleax committed Nov 15, 2017
commit 74afe5cfea875863862368f6af0e85a031e78890
5 changes: 5 additions & 0 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

} else {
var bufs = this.buffers;
buf = (bufs.length === 1 ? bufs[0] : Buffer.concat(bufs, this.nread));
Expand Down Expand Up @@ -485,6 +487,9 @@ function processChunkSync(self, chunk, flushFlag) {

_close(self);

if (nread === 0)
return Buffer.alloc(0);

return (buffers.length === 1 ? buffers[0] : Buffer.concat(buffers, nread));
}

Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-zlib-empty-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const common = require('../common');
const zlib = require('zlib');
const { inspect, promisify } = require('util');
const assert = require('assert');
const emptyBuffer = new Buffer(0);

common.crashOnUnhandledRejection();

(async function() {
for (const [ compress, decompress, method ] of [
[ zlib.deflateRawSync, zlib.inflateRawSync, 'raw sync' ],
[ zlib.deflateSync, zlib.inflateSync, 'deflate sync' ],
[ zlib.gzipSync, zlib.gunzipSync, 'gzip sync' ],
[ promisify(zlib.deflateRaw), promisify(zlib.inflateRaw), 'raw' ],
[ promisify(zlib.deflate), promisify(zlib.inflate), 'deflate' ],
[ promisify(zlib.gzip), promisify(zlib.gunzip), 'gzip' ]
]) {
const compressed = await compress(emptyBuffer);
const decompressed = await decompress(compressed);
assert.deepStrictEqual(
emptyBuffer, decompressed,
`Expected ${inspect(compressed)} to match ${inspect(decompressed)} ` +
`to match for ${method}`);
}
})();