Skip to content

Commit fa01fe6

Browse files
addaleaxMylesBorins
authored andcommitted
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. 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]>
1 parent 035a24e commit fa01fe6

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/zlib.js

+5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ function zlibBufferOnEnd() {
9595
var err;
9696
if (this.nread >= kMaxLength) {
9797
err = new errors.RangeError('ERR_BUFFER_TOO_LARGE');
98+
} else if (this.nread === 0) {
99+
buf = Buffer.alloc(0);
98100
} else {
99101
var bufs = this.buffers;
100102
buf = (bufs.length === 1 ? bufs[0] : Buffer.concat(bufs, this.nread));
@@ -485,6 +487,9 @@ function processChunkSync(self, chunk, flushFlag) {
485487

486488
_close(self);
487489

490+
if (nread === 0)
491+
return Buffer.alloc(0);
492+
488493
return (buffers.length === 1 ? buffers[0] : Buffer.concat(buffers, nread));
489494
}
490495

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const common = require('../common');
3+
const zlib = require('zlib');
4+
const { inspect, promisify } = require('util');
5+
const assert = require('assert');
6+
const emptyBuffer = new Buffer(0);
7+
8+
common.crashOnUnhandledRejection();
9+
10+
(async function() {
11+
for (const [ compress, decompress, method ] of [
12+
[ zlib.deflateRawSync, zlib.inflateRawSync, 'raw sync' ],
13+
[ zlib.deflateSync, zlib.inflateSync, 'deflate sync' ],
14+
[ zlib.gzipSync, zlib.gunzipSync, 'gzip sync' ],
15+
[ promisify(zlib.deflateRaw), promisify(zlib.inflateRaw), 'raw' ],
16+
[ promisify(zlib.deflate), promisify(zlib.inflate), 'deflate' ],
17+
[ promisify(zlib.gzip), promisify(zlib.gunzip), 'gzip' ]
18+
]) {
19+
const compressed = await compress(emptyBuffer);
20+
const decompressed = await decompress(compressed);
21+
assert.deepStrictEqual(
22+
emptyBuffer, decompressed,
23+
`Expected ${inspect(compressed)} to match ${inspect(decompressed)} ` +
24+
`to match for ${method}`);
25+
}
26+
})();

0 commit comments

Comments
 (0)