From f1055e38aa5c6eb18150a031d58c92cb75dd67e3 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Thu, 18 Oct 2018 17:40:57 +0200 Subject: [PATCH 1/4] zlib: do not leak on destroy --- lib/zlib.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/zlib.js b/lib/zlib.js index c97da5dd302928..6abde082aacc34 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -430,6 +430,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 @@ -592,6 +597,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; From 91cdb3dfcdb6cdc4d03773dd07460edf6d4ddfcd Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Mon, 22 Oct 2018 13:59:10 +0200 Subject: [PATCH 2/4] add @mcollina's test case --- test/parallel/test-zlib-destroy.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test/parallel/test-zlib-destroy.js 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); From 9f7b2ce1246396c8b27e5208a8ee5424639c1962 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Mon, 22 Oct 2018 22:31:19 +0200 Subject: [PATCH 3/4] add close in ondata test --- test/parallel/test-zlib-close-in-ondata.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/parallel/test-zlib-close-in-ondata.js 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..23bf075481bfb2 --- /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.allocUnsafe(1024 * 1024 * 20); + +ts.on('data', common.mustCall(() => ts.close())); +ts.end(buf); From 6a34ccdd515ca4c053a8abf85ce39ba19b99aae6 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Tue, 23 Oct 2018 10:00:17 +0200 Subject: [PATCH 4/4] use Buffer.alloc instead of allocUnsafe --- test/parallel/test-zlib-close-in-ondata.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-zlib-close-in-ondata.js b/test/parallel/test-zlib-close-in-ondata.js index 23bf075481bfb2..44d996311dca13 100644 --- a/test/parallel/test-zlib-close-in-ondata.js +++ b/test/parallel/test-zlib-close-in-ondata.js @@ -4,7 +4,7 @@ const common = require('../common'); const zlib = require('zlib'); const ts = zlib.createGzip(); -const buf = Buffer.allocUnsafe(1024 * 1024 * 20); +const buf = Buffer.alloc(1024 * 1024 * 20); ts.on('data', common.mustCall(() => ts.close())); ts.end(buf);