Skip to content

Commit b07cb48

Browse files
mafintoshtargos
authored andcommitted
zlib: do not leak on destroy
PR-URL: #23734 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent dfecf85 commit b07cb48

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

lib/zlib.js

+9
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ Zlib.prototype.close = function close(callback) {
430430
this.destroy();
431431
};
432432

433+
Zlib.prototype._destroy = function _destroy(err, callback) {
434+
_close(this);
435+
callback(err);
436+
};
437+
433438
Zlib.prototype._transform = function _transform(chunk, encoding, cb) {
434439
var flushFlag = this._defaultFlushFlag;
435440
// We use a 'fake' zero-length chunk to carry information about flushes from
@@ -592,6 +597,10 @@ function processCallback() {
592597
assert(false, 'have should not go down');
593598
}
594599

600+
if (self.destroyed) {
601+
return;
602+
}
603+
595604
// exhausted the output buffer, or used all the input create a new one.
596605
if (availOutAfter === 0 || self._outOffset >= self._chunkSize) {
597606
handle.availOutBefore = self._chunkSize;
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const zlib = require('zlib');
5+
6+
const ts = zlib.createGzip();
7+
const buf = Buffer.alloc(1024 * 1024 * 20);
8+
9+
ts.on('data', common.mustCall(() => ts.close()));
10+
ts.end(buf);

test/parallel/test-zlib-destroy.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('assert');
6+
const zlib = require('zlib');
7+
8+
// verify that the zlib transform does clean up
9+
// the handle when calling destroy.
10+
11+
const ts = zlib.createGzip();
12+
ts.destroy();
13+
assert.strictEqual(ts._handle, null);

0 commit comments

Comments
 (0)