Skip to content
forked from nodejs/node

Commit

Permalink
Add some type checking (nodejs#195)
Browse files Browse the repository at this point in the history
 - Check the type before casting to string when creating error objects.
 - Return an error when napi_typeof does not find any valid type.
  • Loading branch information
jasongin committed Mar 23, 2017
1 parent 1427b33 commit 6b4ce53
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1238,8 +1238,11 @@ napi_status napi_create_error(napi_env env,
NAPI_PREAMBLE(env);
CHECK_ARG(result);

v8::Local<v8::Value> 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<v8::String>()));
message_value.As<v8::String>()));

return GET_RETURN_STATUS();
}
Expand All @@ -1250,8 +1253,11 @@ napi_status napi_create_type_error(napi_env env,
NAPI_PREAMBLE(env);
CHECK_ARG(result);

v8::Local<v8::Value> 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<v8::String>()));
message_value.As<v8::String>()));

return GET_RETURN_STATUS();
}
Expand All @@ -1262,8 +1268,11 @@ napi_status napi_create_range_error(napi_env env,
NAPI_PREAMBLE(env);
CHECK_ARG(result);

v8::Local<v8::Value> 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<v8::String>()));
message_value.As<v8::String>()));

return GET_RETURN_STATUS();
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6b4ce53

Please sign in to comment.