Skip to content

Commit f29d8e0

Browse files
mhdawsonMylesBorins
authored andcommittedMay 1, 2018
n-api: add missing exception checking
Add checks for a pending exception in napi_make_callback after the callback has been invoked. If there is a pending exception then we need to avoid checking the result as that will not be able to complete properly. Add additional checks to the unit test for napi_make_callback to catch this case. Backport-PR-URL: #19265 PR-URL: #19362 Fixes: nodejs/node-addon-api#235 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 7c6fa18 commit f29d8e0

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed
 

‎src/node_api.cc

+8-4
Original file line numberDiff line numberDiff line change
@@ -2828,11 +2828,15 @@ napi_status napi_make_callback(napi_env env,
28282828
isolate, v8recv, v8func, argc,
28292829
reinterpret_cast<v8::Local<v8::Value>*>(const_cast<napi_value*>(argv)),
28302830
*node_async_context);
2831-
CHECK_MAYBE_EMPTY(env, callback_result, napi_generic_failure);
28322831

2833-
if (result != nullptr) {
2834-
*result = v8impl::JsValueFromV8LocalValue(
2835-
callback_result.ToLocalChecked());
2832+
if (try_catch.HasCaught()) {
2833+
return napi_set_last_error(env, napi_pending_exception);
2834+
} else {
2835+
CHECK_MAYBE_EMPTY(env, callback_result, napi_generic_failure);
2836+
if (result != nullptr) {
2837+
*result = v8impl::JsValueFromV8LocalValue(
2838+
callback_result.ToLocalChecked());
2839+
}
28362840
}
28372841

28382842
return GET_RETURN_STATUS(env);

‎test/addons-napi/test_make_callback_recurse/binding.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,22 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {
1313
napi_value recv = args[0];
1414
napi_value func = args[1];
1515

16-
napi_make_callback(env, nullptr /* async_context */,
16+
napi_status status = napi_make_callback(env, nullptr /* async_context */,
1717
recv, func, 0 /* argc */, nullptr /* argv */, nullptr /* result */);
1818

19+
bool isExceptionPending;
20+
NAPI_CALL(env, napi_is_exception_pending(env, &isExceptionPending));
21+
if (isExceptionPending && !(status == napi_pending_exception)) {
22+
// if there is an exception pending we don't expect any
23+
// other error
24+
napi_value pending_error;
25+
status = napi_get_and_clear_last_exception(env, &pending_error);
26+
NAPI_CALL(env,
27+
napi_throw_error((env),
28+
nullptr,
29+
"error when only pending exception expected"));
30+
}
31+
1932
return recv;
2033
}
2134

0 commit comments

Comments
 (0)
Please sign in to comment.