Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fa01fe6

Browse filesBrowse 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 <info@bnoordhuis.nl> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 035a24e commit fa01fe6
Copy full SHA for fa01fe6

File tree

Expand file treeCollapse file tree

2 files changed

+31
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+31
-0
lines changed
Open diff view settings
Collapse file

‎lib/zlib.js‎

Copy file name to clipboardExpand all lines: lib/zlib.js
+5Lines changed: 5 additions & 0 deletions
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

Collapse file
+26Lines changed: 26 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.