From 6b4ce5389dbb9642ae5f22ecc2cb2c066913d451 Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Thu, 23 Mar 2017 15:27:09 -0700 Subject: [PATCH] Add some type checking (#195) - Check the type before casting to string when creating error objects. - Return an error when napi_typeof does not find any valid type. --- src/node_api.cc | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index e455027f1851b0..d00468ac0f9f96 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1238,8 +1238,11 @@ napi_status napi_create_error(napi_env env, NAPI_PREAMBLE(env); CHECK_ARG(result); + v8::Local message_value = v8impl::V8LocalValueFromJsValue(msg); + RETURN_STATUS_IF_FALSE(message_value->IsString(), napi_string_expected); + *result = v8impl::JsValueFromV8LocalValue(v8::Exception::Error( - v8impl::V8LocalValueFromJsValue(msg).As())); + message_value.As())); return GET_RETURN_STATUS(); } @@ -1250,8 +1253,11 @@ napi_status napi_create_type_error(napi_env env, NAPI_PREAMBLE(env); CHECK_ARG(result); + v8::Local message_value = v8impl::V8LocalValueFromJsValue(msg); + RETURN_STATUS_IF_FALSE(message_value->IsString(), napi_string_expected); + *result = v8impl::JsValueFromV8LocalValue(v8::Exception::TypeError( - v8impl::V8LocalValueFromJsValue(msg).As())); + message_value.As())); return GET_RETURN_STATUS(); } @@ -1262,8 +1268,11 @@ napi_status napi_create_range_error(napi_env env, NAPI_PREAMBLE(env); CHECK_ARG(result); + v8::Local message_value = v8impl::V8LocalValueFromJsValue(msg); + RETURN_STATUS_IF_FALSE(message_value->IsString(), napi_string_expected); + *result = v8impl::JsValueFromV8LocalValue(v8::Exception::RangeError( - v8impl::V8LocalValueFromJsValue(msg).As())); + message_value.As())); return GET_RETURN_STATUS(); } @@ -1298,7 +1307,8 @@ napi_status napi_typeof(napi_env env, } else if (v->IsExternal()) { *result = napi_external; } else { - *result = napi_object; // Is this correct? + // Should not get here unless V8 has added some new kind of value. + return napi_set_last_error(napi_invalid_arg); } return napi_ok;