Browse Source

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 <info@bnoordhuis.nl>
PR-URL: https://github.com/node-forward/node/pull/37
archived-io.js-v0.12
Fedor Indutny 10 years ago
parent
commit
10d0dbc427
  1. 6
      src/handle_wrap.cc
  2. 6
      src/handle_wrap.h
  3. 15
      src/stream_wrap.cc

6
src/handle_wrap.cc

@ -43,7 +43,7 @@ using v8::Value;
void HandleWrap::Ref(const FunctionCallbackInfo<Value>& args) {
HandleWrap* wrap = Unwrap<HandleWrap>(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<Value>& args) {
void HandleWrap::Unref(const FunctionCallbackInfo<Value>& args) {
HandleWrap* wrap = Unwrap<HandleWrap>(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<Value>& args) {
HandleWrap* wrap = Unwrap<HandleWrap>(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());

6
src/handle_wrap.h

@ -57,7 +57,11 @@ class HandleWrap : public AsyncWrap {
static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Unref(const v8::FunctionCallbackInfo<v8::Value>& 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,

15
src/stream_wrap.cc

@ -90,6 +90,9 @@ void StreamWrap::UpdateWriteQueueSize() {
void StreamWrap::ReadStart(const FunctionCallbackInfo<Value>& args) {
StreamWrap* wrap = Unwrap<StreamWrap>(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<Value>& args) {
void StreamWrap::ReadStop(const FunctionCallbackInfo<Value>& args) {
StreamWrap* wrap = Unwrap<StreamWrap>(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<Value>& args) {
Environment* env = Environment::GetCurrent(args);
StreamWrap* wrap = Unwrap<StreamWrap>(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<Value>& args) {
int err;
StreamWrap* wrap = Unwrap<StreamWrap>(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<Value>& args) {
Environment* env = Environment::GetCurrent(args);
StreamWrap* wrap = Unwrap<StreamWrap>(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<Value>& args) {
void StreamWrap::SetBlocking(const FunctionCallbackInfo<Value>& args) {
StreamWrap* wrap = Unwrap<StreamWrap>(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<Value>& args) {
Environment* env = Environment::GetCurrent(args);
StreamWrap* wrap = Unwrap<StreamWrap>(args.Holder());
if (!IsAlive(wrap))
return args.GetReturnValue().Set(UV_EINVAL);
CHECK(args[0]->IsObject());
Local<Object> req_wrap_obj = args[0].As<Object>();

Loading…
Cancel
Save