Skip to content

Commit

Permalink
zlib: check if the stream is destroyed before push
Browse files Browse the repository at this point in the history
If the stream is destroyed while the transform is still being
applied, push() should not be called, and the internal state
should be cleared.

Refs: koajs/compress#60
PR-URL: #14330
Backport-PR-URL: #14396
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>

 Conflicts:
	lib/zlib.js
  • Loading branch information
mcollina authored and Fishrock123 committed Jul 20, 2017
1 parent 7c3fed5 commit 6fce1a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ Zlib.prototype._processChunk = function _processChunk(chunk, flushFlag, cb) {
if (self._hadError)
return;

if (self.destroyed)
return;

var have = availOutBefore - availOutAfter;
assert(have >= 0, 'have should not go down');

Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-zlib-destroy-pipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const common = require('../common');
const zlib = require('zlib');
const { Writable } = require('stream');

// verify that the zlib transform does not error in case
// it is destroyed with data still in flight

const ts = zlib.createGzip();

const ws = new Writable({
write: common.mustCall((chunk, enc, cb) => {
setImmediate(cb);
ts.destroy();
})
});

const buf = Buffer.allocUnsafe(1024 * 1024 * 20);
ts.end(buf);
ts.pipe(ws);

0 comments on commit 6fce1a3

Please sign in to comment.