Skip to content

Commit c6a43fa

Browse files
mafintoshMylesBorins
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 4749640 commit c6a43fa

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎lib/zlib.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ Zlib.prototype.close = function close(callback) {
427427
this.destroy();
428428
};
429429

430+
Zlib.prototype._destroy=function_destroy(err,callback){
431+
_close(this);
432+
callback(err);
433+
};
434+
430435
Zlib.prototype._transform=function_transform(chunk,encoding,cb){
431436
varflushFlag=this._defaultFlushFlag;
432437
// We use a 'fake' zero-length chunk to carry information about flushes from
@@ -589,6 +594,10 @@ function processCallback() {
589594
assert(false,'have should not go down');
590595
}
591596

597+
if(self.destroyed){
598+
return;
599+
}
600+
592601
// exhausted the output buffer, or used all the input create a new one.
593602
if(availOutAfter===0||self._outOffset>=self._chunkSize){
594603
handle.availOutBefore=self._chunkSize;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constzlib=require('zlib');
5+
6+
constts=zlib.createGzip();
7+
constbuf=Buffer.alloc(1024*1024*20);
8+
9+
ts.on('data',common.mustCall(()=>ts.close()));
10+
ts.end(buf);

‎test/parallel/test-zlib-destroy.js‎

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

0 commit comments

Comments
 (0)