summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFedor Indutny <fedor.indutny@gmail.com>2014-02-13 17:17:59 +0400
committerFedor Indutny <fedor.indutny@gmail.com>2014-02-18 01:11:05 +0400
commit829a9b8cba0680f1d451551e12491e7b0e1ea92f (patch)
tree2f03648b355b4167210bfaf253bfe0ecb006706c
parent86b8d84811484763b251b9a8a2b9e673964ea6b5 (diff)
downloadnode-829a9b8cba0680f1d451551e12491e7b0e1ea92f.tar.gz
zlib: introduce pending close state
zlib should not crash in `close()` if the write is still in progress. fix #7101
-rw-r--r--src/node_zlib.cc15
-rw-r--r--test/simple/test-zlib-close-after-write.js38
2 files changed, 52 insertions, 1 deletions
diff --git a/src/node_zlib.cc b/src/node_zlib.cc
index d52267628..5c8578d86 100644
--- a/src/node_zlib.cc
+++ b/src/node_zlib.cc
@@ -72,18 +72,25 @@ class ZCtx : public ObjectWrap {
, flush_(0)
, chunk_size_(0)
, write_in_progress_(false)
+ , pending_close_(false)
, mode_(mode)
{
}
~ZCtx() {
+ assert(!write_in_progress_ && "write in progress");
Close();
}
void Close() {
- assert(!write_in_progress_ && "write in progress");
+ if (write_in_progress_) {
+ pending_close_ = true;
+ return;
+ }
+
+ pending_close_ = false;
assert(init_done_ && "close before init");
assert(mode_ <= UNZIP);
@@ -122,6 +129,7 @@ class ZCtx : public ObjectWrap {
assert(ctx->mode_ != NONE && "already finalized");
assert(!ctx->write_in_progress_ && "write already in progress");
+ assert(!ctx->pending_close_ && "close is pending");
ctx->write_in_progress_ = true;
ctx->Ref();
@@ -279,6 +287,8 @@ class ZCtx : public ObjectWrap {
MakeCallback(ctx->handle_, callback_sym, ARRAY_SIZE(args), args);
ctx->Unref();
+ if (ctx->pending_close_)
+ ctx->Close();
}
static void Error(ZCtx *ctx, const char *msg_) {
@@ -299,6 +309,8 @@ class ZCtx : public ObjectWrap {
// no hope of rescue.
ctx->write_in_progress_ = false;
ctx->Unref();
+ if (ctx->pending_close_)
+ ctx->Close();
}
static Handle<Value> New(const Arguments& args) {
@@ -495,6 +507,7 @@ class ZCtx : public ObjectWrap {
int chunk_size_;
bool write_in_progress_;
+ bool pending_close_;
uv_work_t work_req_;
node_zlib_mode mode_;
diff --git a/test/simple/test-zlib-close-after-write.js b/test/simple/test-zlib-close-after-write.js
new file mode 100644
index 000000000..f0f176008
--- /dev/null
+++ b/test/simple/test-zlib-close-after-write.js
@@ -0,0 +1,38 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common.js');
+var assert = require('assert');
+var zlib = require('zlib');
+
+var closed = false;
+
+zlib.gzip('hello', function(err, out) {
+ var unzip = zlib.createGunzip();
+ unzip.write(out);
+ unzip.close(function() {
+ closed = true;
+ });
+});
+
+process.on('exit', function() {
+ assert(closed);
+});