Skip to content

Commit

Permalink
zlib: do not leak on destroy
Browse files Browse the repository at this point in the history
PR-URL: #23734
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
mafintosh authored and MylesBorins committed Dec 26, 2018
1 parent 4749640 commit c6a43fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ Zlib.prototype.close = function close(callback) {
this.destroy();
};

Zlib.prototype._destroy = function _destroy(err, callback) {
_close(this);
callback(err);
};

Zlib.prototype._transform = function _transform(chunk, encoding, cb) {
var flushFlag = this._defaultFlushFlag;
// We use a 'fake' zero-length chunk to carry information about flushes from
Expand Down Expand Up @@ -589,6 +594,10 @@ function processCallback() {
assert(false, 'have should not go down');
}

if (self.destroyed) {
return;
}

// exhausted the output buffer, or used all the input create a new one.
if (availOutAfter === 0 || self._outOffset >= self._chunkSize) {
handle.availOutBefore = self._chunkSize;
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-zlib-close-in-ondata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const common = require('../common');
const zlib = require('zlib');

const ts = zlib.createGzip();
const buf = Buffer.alloc(1024 * 1024 * 20);

ts.on('data', common.mustCall(() => ts.close()));
ts.end(buf);
13 changes: 13 additions & 0 deletions test/parallel/test-zlib-destroy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

require('../common');

const assert = require('assert');
const zlib = require('zlib');

// verify that the zlib transform does clean up
// the handle when calling destroy.

const ts = zlib.createGzip();
ts.destroy();
assert.strictEqual(ts._handle, null);

0 comments on commit c6a43fa

Please sign in to comment.