diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 8f97ca4d9cbb84..cc2312bd2fd1ed 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -2520,21 +2520,23 @@ napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env, v8::Local val = v8impl::V8LocalValueFromJsValue(value); RETURN_STATUS_IF_FALSE(env, val->IsString(), napi_string_expected); + v8::Local str = val.As(); if (!buf) { CHECK_ARG(env, result); // V8 assumes UTF-16 length is the same as the number of characters. - *result = val.As()->Length(); + *result = str->Length(); } else if (bufsize != 0) { - int copied = val.As()->Write(env->isolate, - reinterpret_cast(buf), - 0, - bufsize - 1, - v8::String::NO_NULL_TERMINATION); + uint32_t length = static_cast( + std::min(bufsize - 1, static_cast(str->Length()))); + str->WriteV2(env->isolate, + 0, + length, + reinterpret_cast(buf), + v8::String::WriteFlags::kNullTerminate); - buf[copied] = '\0'; if (result != nullptr) { - *result = copied; + *result = length; } } else if (result != nullptr) { *result = 0;