diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index 099e105334cedf..e8e73f007ef712 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -77,8 +77,13 @@ void StreamBase::AddMethods(Environment* env, template void StreamBase::GetFD(Local key, const PropertyCallbackInfo& args) { - StreamBase* wrap = Unwrap(args.Holder()); + Base* handle = Unwrap(args.Holder()); + // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD(). + if (handle == nullptr) + return args.GetReturnValue().Set(-1); + + StreamBase* wrap = static_cast(handle); if (!wrap->IsAlive()) return args.GetReturnValue().Set(UV_EINVAL); @@ -89,8 +94,13 @@ void StreamBase::GetFD(Local key, template void StreamBase::GetBytesRead(Local key, const PropertyCallbackInfo& args) { - StreamBase* wrap = Unwrap(args.Holder()); + Base* handle = Unwrap(args.Holder()); + + // The handle instance hasn't been set. So no bytes could have been read. + if (handle == nullptr) + return args.GetReturnValue().Set(0); + StreamBase* wrap = static_cast(handle); // uint64_t -> double. 53bits is enough for all real cases. args.GetReturnValue().Set(static_cast(wrap->bytes_read_)); } @@ -99,8 +109,12 @@ void StreamBase::GetBytesRead(Local key, template void StreamBase::GetExternal(Local key, const PropertyCallbackInfo& args) { - StreamBase* wrap = Unwrap(args.Holder()); + Base* handle = Unwrap(args.Holder()); + if (handle == nullptr) + return args.GetReturnValue().SetUndefined(); + + StreamBase* wrap = static_cast(handle); Local ext = External::New(args.GetIsolate(), wrap); args.GetReturnValue().Set(ext); } @@ -109,8 +123,12 @@ void StreamBase::GetExternal(Local key, template & args)> void StreamBase::JSMethod(const FunctionCallbackInfo& args) { - StreamBase* wrap = Unwrap(args.Holder()); + Base* handle = Unwrap(args.Holder()); + + if (handle == nullptr) + return args.GetReturnValue().SetUndefined(); + StreamBase* wrap = static_cast(handle); if (!wrap->IsAlive()) return args.GetReturnValue().Set(UV_EINVAL);