Skip to content

Commit

Permalink
src: Avoid calling into C++ with a null this
Browse files Browse the repository at this point in the history
when exceptions are off
  • Loading branch information
chearon committed May 2, 2023
1 parent 0e34f22 commit 51865f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
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

0 comments on commit 51865f3

Please sign in to comment.