Skip to content

Commit 19d6f06

Browse files
trevnorrisMylesBorins
authored andcommitted
stream_base: always use Base template class
First cast the pointer to the child Base class before casting to the parent class to make sure it returns the correct pointer. PR-URL: #6184 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 6076293 commit 19d6f06

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/stream_base-inl.h

+22-4
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ void StreamBase::AddMethods(Environment* env,
7777
template <class Base>
7878
void 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,
8994
template <class Base>
9095
void 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,
99109
template <class Base>
100110
void 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,
109123
template <class Base,
110124
int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
111125
void 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

Comments
 (0)