Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: Avoid calling into C++ with a null this #1313

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ napi_value TemplatedInstanceCallback(napi_env env,
return details::WrapCallback([&] {
CallbackInfo cbInfo(env, info);
T* instance = T::Unwrap(cbInfo.This().As<Object>());
return (instance->*UnwrapCallback)(cbInfo);
return instance ? (instance->*UnwrapCallback)(cbInfo) : Napi::Value();
});
}

Expand All @@ -175,7 +175,7 @@ napi_value TemplatedInstanceVoidCallback(napi_env env, napi_callback_info info)
return details::WrapCallback([&] {
CallbackInfo cbInfo(env, info);
T* instance = T::Unwrap(cbInfo.This().As<Object>());
(instance->*UnwrapCallback)(cbInfo);
if (instance) (instance->*UnwrapCallback)(cbInfo);
return nullptr;
});
}
Expand Down Expand Up @@ -4340,7 +4340,7 @@ inline napi_value InstanceWrap<T>::InstanceVoidMethodCallbackWrapper(
callbackInfo.SetData(callbackData->data);
T* instance = T::Unwrap(callbackInfo.This().As<Object>());
auto cb = callbackData->callback;
(instance->*cb)(callbackInfo);
if (instance) (instance->*cb)(callbackInfo);
return nullptr;
});
}
Expand All @@ -4355,7 +4355,7 @@ inline napi_value InstanceWrap<T>::InstanceMethodCallbackWrapper(
callbackInfo.SetData(callbackData->data);
T* instance = T::Unwrap(callbackInfo.This().As<Object>());
auto cb = callbackData->callback;
return (instance->*cb)(callbackInfo);
return instance ? (instance->*cb)(callbackInfo) : Napi::Value();
});
}

Expand All @@ -4369,7 +4369,7 @@ inline napi_value InstanceWrap<T>::InstanceGetterCallbackWrapper(
callbackInfo.SetData(callbackData->data);
T* instance = T::Unwrap(callbackInfo.This().As<Object>());
auto cb = callbackData->getterCallback;
return (instance->*cb)(callbackInfo);
return instance ? (instance->*cb)(callbackInfo) : Napi::Value();
});
}

Expand All @@ -4383,7 +4383,7 @@ inline napi_value InstanceWrap<T>::InstanceSetterCallbackWrapper(
callbackInfo.SetData(callbackData->data);
T* instance = T::Unwrap(callbackInfo.This().As<Object>());
auto cb = callbackData->setterCallback;
(instance->*cb)(callbackInfo, callbackInfo[0]);
if (instance) (instance->*cb)(callbackInfo, callbackInfo[0]);
return nullptr;
});
}
Expand All @@ -4395,7 +4395,7 @@ inline napi_value InstanceWrap<T>::WrappedMethod(
return details::WrapCallback([&] {
const CallbackInfo cbInfo(env, info);
T* instance = T::Unwrap(cbInfo.This().As<Object>());
(instance->*method)(cbInfo, cbInfo[0]);
if (instance) (instance->*method)(cbInfo, cbInfo[0]);
return nullptr;
});
}
Expand Down
9 changes: 9 additions & 0 deletions test/objectwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ async function test (binding) {
obj.testSetter = 'instance getter 2';
assert.strictEqual(obj.testGetter, 'instance getter 2');
assert.strictEqual(obj.testGetterT, 'instance getter 2');

assert.throws(() => clazz.prototype.testGetter, /Invalid argument/);
assert.throws(() => clazz.prototype.testGetterT, /Invalid argument/);
}

// read write-only
Expand Down Expand Up @@ -61,6 +64,9 @@ async function test (binding) {

obj.testGetSetT = 'instance getset 4';
assert.strictEqual(obj.testGetSetT, 'instance getset 4');

assert.throws(() => { clazz.prototype.testGetSet = 'instance getset'; }, /Invalid argument/);
assert.throws(() => { clazz.prototype.testGetSetT = 'instance getset'; }, /Invalid argument/);
}

// rw symbol
Expand Down Expand Up @@ -98,6 +104,9 @@ async function test (binding) {
assert.strictEqual(obj.testMethodT(), 'method<>(const char*)');
obj[clazz.kTestVoidMethodTInternal]('method<>(Symbol)');
assert.strictEqual(obj[clazz.kTestMethodTInternal](), 'method<>(Symbol)');
assert.throws(() => clazz.prototype.testMethod('method'));
assert.throws(() => clazz.prototype.testMethodT());
assert.throws(() => clazz.prototype.testVoidMethodT('method<>(const char*)'));
};

const testEnumerables = (obj, clazz) => {
Expand Down