fix(napi): correctly handle name in napi_create_function (#17489)

Fixes https://github.com/denoland/deno/issues/17472
This commit is contained in:
Divy Srivastava 2023-01-22 01:34:18 -08:00 committed by GitHub
parent 5928925541
commit c3e3694b9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 13 deletions

View file

@ -597,7 +597,7 @@ fn napi_create_external_buffer(
#[napi_sym::napi_sym]
fn napi_create_function(
env: *mut Env,
name: *const u8,
name: *const c_char,
length: usize,
cb: napi_callback,
cb_info: napi_callback_info,
@ -606,21 +606,17 @@ fn napi_create_function(
check_env!(env);
check_arg!(env, result);
check_arg_option!(env, cb);
check_arg!(env, name);
if length > INT_MAX as _ {
return Err(Error::InvalidArg);
}
let name = std::slice::from_raw_parts(name, length);
// If it ends with NULL
let name = if name[name.len() - 1] == 0 {
std::str::from_utf8_unchecked(&name[0..name.len() - 1])
} else {
std::str::from_utf8_unchecked(name)
};
let name = name
.as_ref()
.map(|_| check_new_from_utf8_len(env, name, length))
.transpose()?;
*result = create_function(env, Some(name), cb, cb_info).into();
*result = create_function(env, name, cb, cb_info).into();
Ok(())
}

View file

@ -51,7 +51,7 @@ extern "C" fn call_fn(info: *const v8::FunctionCallbackInfo) {
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn create_function<'a>(
env_ptr: *mut Env,
name: Option<&str>,
name: Option<v8::Local<v8::String>>,
cb: napi_callback,
cb_info: napi_callback_info,
) -> v8::Local<'a, v8::Function> {
@ -67,8 +67,7 @@ pub fn create_function<'a>(
.build(scope)
.unwrap();
if let Some(name) = name {
let v8str = v8::String::new(scope, name).unwrap();
if let Some(v8str) = name {
function.set_name(v8str);
}