GeezerVison is showing github.com — zlib: fix memory leak for invalid input · nodejs/node@8e542ea
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_zlib.cc +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -215,8 +215,8 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { |
215 | 215 | ctx->write_result_[0] = ctx->strm_.avail_out; |
216 | 216 | ctx->write_result_[1] = ctx->strm_.avail_in; |
217 | 217 | ctx->write_in_progress_ = false; |
218 | | - ctx->Unref(); |
219 | 218 | } |
| 219 | + ctx->Unref(); |
220 | 220 | return; |
221 | 221 | } |
222 | 222 |
|
@@ -364,6 +364,7 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { |
364 | 364 | // v8 land! |
365 | 365 | void AfterThreadPoolWork(int status) override { |
366 | 366 | AllocScope alloc_scope(this); |
| 367 | + OnScopeLeave on_scope_leave([&]() { Unref(); }); |
367 | 368 |
|
368 | 369 | write_in_progress_ = false; |
369 | 370 |
|
@@ -388,7 +389,6 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { |
388 | 389 | write_js_callback_); |
389 | 390 | MakeCallback(cb, 0, nullptr); |
390 | 391 |
|
391 | | - Unref(); |
392 | 392 | if (pending_close_) |
393 | 393 | Close(); |
394 | 394 | } |
@@ -410,8 +410,6 @@ class ZCtx : public AsyncWrap, public ThreadPoolWork { |
410 | 410 | MakeCallback(env()->onerror_string(), arraysize(args), args); |
411 | 411 |
|
412 | 412 | // no hope of rescue. |
413 | | - if (write_in_progress_) |
414 | | - Unref(); |
415 | 413 | write_in_progress_ = false; |
416 | 414 | if (pending_close_) |
417 | 415 | Close(); |
|
Collapse file
Copy file name to clipboard +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// Flags: --expose-gc |
| 2 | +'use strict'; |
| 3 | +const common = require('../common'); |
| 4 | +const onGC = require('../common/ongc'); |
| 5 | +const assert = require('assert'); |
| 6 | +const zlib = require('zlib'); |
| 7 | + |
| 8 | +// Checks that, if a zlib context fails with an error, it can still be GC'ed: |
| 9 | +// Refs: https://github.com/nodejs/node/issues/22705 |
| 10 | + |
| 11 | +const ongc = common.mustCall(); |
| 12 | + |
| 13 | +{ |
| 14 | + const input = Buffer.from('foobar'); |
| 15 | + const strm = zlib.createInflate(); |
| 16 | + strm.end(input); |
| 17 | + strm.once('error', common.mustCall((err) => { |
| 18 | + assert(err); |
| 19 | + setImmediate(() => { |
| 20 | + global.gc(); |
| 21 | + // Keep the event loop alive for seeing the async_hooks destroy hook |
| 22 | + // we use for GC tracking... |
| 23 | + // TODO(addaleax): This should maybe not be necessary? |
| 24 | + setImmediate(() => {}); |
| 25 | + }); |
| 26 | + })); |
| 27 | + onGC(strm, { ongc }); |
| 28 | +} |