From c6a43fa2ef80aff8efdb1f58c848e798ddaa9e66 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Thu, 18 Oct 2018 17:40:57 +0200 Subject: [PATCH] zlib: do not leak on destroy PR-URL: https://github.com/nodejs/node/pull/23734 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Matteo Collina --- lib/zlib.js | 9 +++++++++ test/parallel/test-zlib-close-in-ondata.js | 10 ++++++++++ test/parallel/test-zlib-destroy.js | 13 +++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 test/parallel/test-zlib-close-in-ondata.js create mode 100644 test/parallel/test-zlib-destroy.js diff --git a/lib/zlib.js b/lib/zlib.js index 97b12410f121ef..9336b52f85313b 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -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 @@ -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; diff --git a/test/parallel/test-zlib-close-in-ondata.js b/test/parallel/test-zlib-close-in-ondata.js new file mode 100644 index 00000000000000..44d996311dca13 --- /dev/null +++ b/test/parallel/test-zlib-close-in-ondata.js @@ -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); diff --git a/test/parallel/test-zlib-destroy.js b/test/parallel/test-zlib-destroy.js new file mode 100644 index 00000000000000..d8eab42186a5fa --- /dev/null +++ b/test/parallel/test-zlib-destroy.js @@ -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);