@@ -77,8 +77,13 @@ void StreamBase::AddMethods(Environment* env,
7777template <class Base >
7878void StreamBase::GetFD (Local<String> key,
7979 const PropertyCallbackInfo<Value>& args) {
80- StreamBase* wrap = Unwrap<Base>(args.Holder ());
80+ Base* handle = Unwrap<Base>(args.Holder ());
8181
82+ // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
83+ if (handle == nullptr )
84+ return args.GetReturnValue ().Set (-1 );
85+
86+ StreamBase* wrap = static_cast <StreamBase*>(handle);
8287 if (!wrap->IsAlive ())
8388 return args.GetReturnValue ().Set (UV_EINVAL);
8489
@@ -89,8 +94,13 @@ void StreamBase::GetFD(Local<String> key,
8994template <class Base >
9095void StreamBase::GetBytesRead (Local<String> key,
9196 const PropertyCallbackInfo<Value>& args) {
92- StreamBase* wrap = Unwrap<Base>(args.Holder ());
97+ Base* handle = Unwrap<Base>(args.Holder ());
98+
99+ // The handle instance hasn't been set. So no bytes could have been read.
100+ if (handle == nullptr )
101+ return args.GetReturnValue ().Set (0 );
93102
103+ StreamBase* wrap = static_cast <StreamBase*>(handle);
94104 // uint64_t -> double. 53bits is enough for all real cases.
95105 args.GetReturnValue ().Set (static_cast <double >(wrap->bytes_read_ ));
96106}
@@ -99,8 +109,12 @@ void StreamBase::GetBytesRead(Local<String> key,
99109template <class Base >
100110void StreamBase::GetExternal (Local<String> key,
101111 const PropertyCallbackInfo<Value>& args) {
102- StreamBase* wrap = Unwrap<Base>(args.Holder ());
112+ Base* handle = Unwrap<Base>(args.Holder ());
103113
114+ if (handle == nullptr )
115+ return args.GetReturnValue ().SetUndefined ();
116+
117+ StreamBase* wrap = static_cast <StreamBase*>(handle);
104118 Local<External> ext = External::New (args.GetIsolate (), wrap);
105119 args.GetReturnValue ().Set (ext);
106120}
@@ -109,8 +123,12 @@ void StreamBase::GetExternal(Local<String> key,
109123template <class Base ,
110124 int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
111125void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
112- StreamBase* wrap = Unwrap<Base>(args.Holder ());
126+ Base* handle = Unwrap<Base>(args.Holder ());
127+
128+ if (handle == nullptr )
129+ return args.GetReturnValue ().SetUndefined ();
113130
131+ StreamBase* wrap = static_cast <StreamBase*>(handle);
114132 if (!wrap->IsAlive ())
115133 return args.GetReturnValue ().Set (UV_EINVAL);
116134
0 commit comments