From 19d6f06058c1b8419c1c996b9fad15f7fce72bb3 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Wed, 11 May 2016 01:03:44 -0600 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/6184 Reviewed-By: Ben Noordhuis Reviewed-By: Anna Henningsen --- src/stream_base-inl.h | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) 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);