Skip to content

Commit

Permalink
stream_base: always use Base template class
Browse files Browse the repository at this point in the history
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 <info@bnoordhuis.nl>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
trevnorris authored and MylesBorins committed Oct 26, 2016
1 parent 6076293 commit 19d6f06
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/stream_base-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ void StreamBase::AddMethods(Environment* env,
template <class Base>
void StreamBase::GetFD(Local<String> key,
const PropertyCallbackInfo<Value>& args) {
StreamBase* wrap = Unwrap<Base>(args.Holder());
Base* handle = Unwrap<Base>(args.Holder());

// Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
if (handle == nullptr)
return args.GetReturnValue().Set(-1);

StreamBase* wrap = static_cast<StreamBase*>(handle);
if (!wrap->IsAlive())
return args.GetReturnValue().Set(UV_EINVAL);

Expand All @@ -89,8 +94,13 @@ void StreamBase::GetFD(Local<String> key,
template <class Base>
void StreamBase::GetBytesRead(Local<String> key,
const PropertyCallbackInfo<Value>& args) {
StreamBase* wrap = Unwrap<Base>(args.Holder());
Base* handle = Unwrap<Base>(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<StreamBase*>(handle);
// uint64_t -> double. 53bits is enough for all real cases.
args.GetReturnValue().Set(static_cast<double>(wrap->bytes_read_));
}
Expand All @@ -99,8 +109,12 @@ void StreamBase::GetBytesRead(Local<String> key,
template <class Base>
void StreamBase::GetExternal(Local<String> key,
const PropertyCallbackInfo<Value>& args) {
StreamBase* wrap = Unwrap<Base>(args.Holder());
Base* handle = Unwrap<Base>(args.Holder());

if (handle == nullptr)
return args.GetReturnValue().SetUndefined();

StreamBase* wrap = static_cast<StreamBase*>(handle);
Local<External> ext = External::New(args.GetIsolate(), wrap);
args.GetReturnValue().Set(ext);
}
Expand All @@ -109,8 +123,12 @@ void StreamBase::GetExternal(Local<String> key,
template <class Base,
int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
StreamBase* wrap = Unwrap<Base>(args.Holder());
Base* handle = Unwrap<Base>(args.Holder());

if (handle == nullptr)
return args.GetReturnValue().SetUndefined();

StreamBase* wrap = static_cast<StreamBase*>(handle);
if (!wrap->IsAlive())
return args.GetReturnValue().Set(UV_EINVAL);

Expand Down

0 comments on commit 19d6f06

Please sign in to comment.