summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTrevor Norris <trev.norris@gmail.com>2016-05-11 01:03:44 -0600
committerRod Vagg <rod@vagg.org>2016-06-02 22:39:22 +1000
commit0d08fc415f12a4ea8babfad0d72ccda50a3e2c25 (patch)
treeebd171a0799bf225f119d945496f9d1d69bde4eb /src
parent5779ed2a4a14b7cebb75362f537bf252703095c3 (diff)
downloadnode-new-0d08fc415f12a4ea8babfad0d72ccda50a3e2c25.tar.gz
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 <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'src')
-rw-r--r--src/stream_base-inl.h26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h
index 099e105334..e8e73f007e 100644
--- a/src/stream_base-inl.h
+++ b/src/stream_base-inl.h
@@ -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);
@@ -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_));
}
@@ -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);
}
@@ -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);