From 10d0dbc4274dccdcc59ed6d2cd70e7e4267453ee Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 24 Oct 2014 19:46:25 +0400 Subject: [PATCH] stream_wrap: do not crash if handle was closed Ignore cases where the handle is already gone, like we do in `handle_wrap.cc`. It should be safe to close handle and then call some binding methods on it, since the internal handle may be shared between `_tls_wrap.js` and `net.js` modules. Reviewed-By: Ben Noordhuis PR-URL: https://github.com/node-forward/node/pull/37 --- src/handle_wrap.cc | 6 +++--- src/handle_wrap.h | 6 +++++- src/stream_wrap.cc | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/handle_wrap.cc b/src/handle_wrap.cc index 595ea7d3a53..3e348a8b258 100644 --- a/src/handle_wrap.cc +++ b/src/handle_wrap.cc @@ -43,7 +43,7 @@ using v8::Value; void HandleWrap::Ref(const FunctionCallbackInfo& args) { HandleWrap* wrap = Unwrap(args.Holder()); - if (wrap != nullptr && wrap->handle__ != nullptr) { + if (IsAlive(wrap)) { uv_ref(wrap->handle__); wrap->flags_ &= ~kUnref; } @@ -53,7 +53,7 @@ void HandleWrap::Ref(const FunctionCallbackInfo& args) { void HandleWrap::Unref(const FunctionCallbackInfo& args) { HandleWrap* wrap = Unwrap(args.Holder()); - if (wrap != nullptr && wrap->handle__ != nullptr) { + if (IsAlive(wrap)) { uv_unref(wrap->handle__); wrap->flags_ |= kUnref; } @@ -66,7 +66,7 @@ void HandleWrap::Close(const FunctionCallbackInfo& args) { HandleWrap* wrap = Unwrap(args.Holder()); // guard against uninitialized handle or double close - if (wrap == nullptr || wrap->handle__ == nullptr) + if (!IsAlive(wrap)) return; CHECK_EQ(false, wrap->persistent().IsEmpty()); diff --git a/src/handle_wrap.h b/src/handle_wrap.h index 40ee75cd811..13457e7cc53 100644 --- a/src/handle_wrap.h +++ b/src/handle_wrap.h @@ -57,7 +57,11 @@ class HandleWrap : public AsyncWrap { static void Ref(const v8::FunctionCallbackInfo& args); static void Unref(const v8::FunctionCallbackInfo& args); - inline uv_handle_t* GetHandle() { return handle__; } + static inline bool IsAlive(const HandleWrap* wrap) { + return wrap != nullptr && wrap->GetHandle() != nullptr; + } + + inline uv_handle_t* GetHandle() const { return handle__; } protected: HandleWrap(Environment* env, diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index a6c64ac8bd4..103d1e30381 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -90,6 +90,9 @@ void StreamWrap::UpdateWriteQueueSize() { void StreamWrap::ReadStart(const FunctionCallbackInfo& args) { StreamWrap* wrap = Unwrap(args.Holder()); + if (!IsAlive(wrap)) + return args.GetReturnValue().Set(UV_EINVAL); + int err = uv_read_start(wrap->stream(), OnAlloc, OnRead); args.GetReturnValue().Set(err); } @@ -97,6 +100,8 @@ void StreamWrap::ReadStart(const FunctionCallbackInfo& args) { void StreamWrap::ReadStop(const FunctionCallbackInfo& args) { StreamWrap* wrap = Unwrap(args.Holder()); + if (!IsAlive(wrap)) + return args.GetReturnValue().Set(UV_EINVAL); int err = uv_read_stop(wrap->stream()); args.GetReturnValue().Set(err); } @@ -183,6 +188,8 @@ void StreamWrap::WriteBuffer(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); StreamWrap* wrap = Unwrap(args.Holder()); + if (!IsAlive(wrap)) + return args.GetReturnValue().Set(UV_EINVAL); CHECK(args[0]->IsObject()); CHECK(Buffer::HasInstance(args[1])); @@ -240,6 +247,8 @@ void StreamWrap::WriteStringImpl(const FunctionCallbackInfo& args) { int err; StreamWrap* wrap = Unwrap(args.Holder()); + if (!IsAlive(wrap)) + return args.GetReturnValue().Set(UV_EINVAL); CHECK(args[0]->IsObject()); CHECK(args[1]->IsString()); @@ -367,6 +376,8 @@ void StreamWrap::Writev(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); StreamWrap* wrap = Unwrap(args.Holder()); + if (!IsAlive(wrap)) + return args.GetReturnValue().Set(UV_EINVAL); CHECK(args[0]->IsObject()); CHECK(args[1]->IsArray()); @@ -493,6 +504,8 @@ void StreamWrap::WriteBinaryString(const FunctionCallbackInfo& args) { void StreamWrap::SetBlocking(const FunctionCallbackInfo& args) { StreamWrap* wrap = Unwrap(args.Holder()); + if (!IsAlive(wrap)) + return args.GetReturnValue().Set(UV_EINVAL); CHECK_GT(args.Length(), 0); int err = uv_stream_set_blocking(wrap->stream(), args[0]->IsTrue()); args.GetReturnValue().Set(err); @@ -537,6 +550,8 @@ void StreamWrap::Shutdown(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); StreamWrap* wrap = Unwrap(args.Holder()); + if (!IsAlive(wrap)) + return args.GetReturnValue().Set(UV_EINVAL); CHECK(args[0]->IsObject()); Local req_wrap_obj = args[0].As();