From 74ca470d2c7300e1756450a3f1c2d2cb4efa4705 Mon Sep 17 00:00:00 2001 From: yys Date: Sat, 26 Oct 2024 15:19:50 +0800 Subject: [PATCH] Modify napi_add_env_cleanup_hook call method Signed-off-by: yys --- .../js/napi/http/http_module/src/http_module.cpp | 8 +++++++- .../net_ssl/net_ssl_module/src/net_ssl_module.cpp | 8 +++++++- .../napi/socket/socket_module/src/socket_module.cpp | 8 +++++++- .../websocket_module/src/websocket_module.cpp | 8 +++++++- utils/napi_utils/src/napi_utils.cpp | 12 +++++++++++- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/frameworks/js/napi/http/http_module/src/http_module.cpp b/frameworks/js/napi/http/http_module/src/http_module.cpp index 66bd1e1e..f5ae353a 100644 --- a/frameworks/js/napi/http/http_module/src/http_module.cpp +++ b/frameworks/js/napi/http/http_module/src/http_module.cpp @@ -57,7 +57,13 @@ napi_value HttpModuleExports::InitHttpModule(napi_env env, napi_value exports) InitHttpProperties(env, exports); g_moduleId = NapiUtils::CreateUvHandlerQueue(env); NapiUtils::SetEnvValid(env); - napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, env); + auto envWrapper = new (std::nothrow) napi_env; + if (envWrapper == nullptr) { + NETSTACK_LOGE("new napi env fail"); + return exports; + } + *envWrapper = env; + napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, envWrapper); std::call_once(g_isAtomicServiceFlag, []() { g_appIsAtomicService = CommonUtils::IsAtomicService(g_appBundleName); NETSTACK_LOGI("IsAtomicService bundleName is %{public}s, isAtomicService is %{public}d", diff --git a/frameworks/js/napi/net_ssl/net_ssl_module/src/net_ssl_module.cpp b/frameworks/js/napi/net_ssl/net_ssl_module/src/net_ssl_module.cpp index 321fc9cd..086783e1 100644 --- a/frameworks/js/napi/net_ssl/net_ssl_module/src/net_ssl_module.cpp +++ b/frameworks/js/napi/net_ssl/net_ssl_module/src/net_ssl_module.cpp @@ -41,7 +41,13 @@ napi_value NetSslModuleExports::InitNetSslModule(napi_env env, napi_value export { InitSslProperties(env, exports); NapiUtils::SetEnvValid(env); - napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, env); + auto envWrapper = new (std::nothrow) napi_env; + if (envWrapper == nullptr) { + NETSTACK_LOGE("new napi env fail"); + return exports; + } + *envWrapper = env; + napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, envWrapper); return exports; } diff --git a/frameworks/js/napi/socket/socket_module/src/socket_module.cpp b/frameworks/js/napi/socket/socket_module/src/socket_module.cpp index edaba9f7..333d92da 100644 --- a/frameworks/js/napi/socket/socket_module/src/socket_module.cpp +++ b/frameworks/js/napi/socket/socket_module/src/socket_module.cpp @@ -456,7 +456,13 @@ napi_value SocketModuleExports::InitSocketModule(napi_env env, napi_value export DefineLocalSocketServerClass(env, exports); InitSocketProperties(env, exports); NapiUtils::SetEnvValid(env); - napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, env); + auto envWrapper = new (std::nothrow) napi_env; + if (envWrapper == nullptr) { + NETSTACK_LOGE("new napi env fail"); + return exports; + } + *envWrapper = env; + napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, envWrapper); return exports; } diff --git a/frameworks/js/napi/websocket/websocket_module/src/websocket_module.cpp b/frameworks/js/napi/websocket/websocket_module/src/websocket_module.cpp index 4df190f2..e32ada82 100644 --- a/frameworks/js/napi/websocket/websocket_module/src/websocket_module.cpp +++ b/frameworks/js/napi/websocket/websocket_module/src/websocket_module.cpp @@ -31,7 +31,13 @@ napi_value WebSocketModule::InitWebSocketModule(napi_env env, napi_value exports DefineWebSocketClass(env, exports); InitWebSocketProperties(env, exports); NapiUtils::SetEnvValid(env); - napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, env); + auto envWrapper = new (std::nothrow) napi_env; + if (envWrapper == nullptr) { + NETSTACK_LOGE("new napi env fail"); + return exports; + } + *envWrapper = env; + napi_add_env_cleanup_hook(env, NapiUtils::HookForEnvCleanup, envWrapper); std::call_once(g_isAtomicServiceFlag, []() { g_appIsAtomicService = CommonUtils::IsAtomicService(g_appBundleName); NETSTACK_LOGI("IsAtomicService bundleName is %{public}s, isAtomicService is %{public}d", diff --git a/utils/napi_utils/src/napi_utils.cpp b/utils/napi_utils/src/napi_utils.cpp index 512fa7d8..cf5d7a6b 100644 --- a/utils/napi_utils/src/napi_utils.cpp +++ b/utils/napi_utils/src/napi_utils.cpp @@ -875,7 +875,17 @@ void UvHandlerQueue::Push(const UvHandler &handler) void HookForEnvCleanup(void *data) { std::lock_guard lock(mutexForEnv); - auto env = static_cast(data); + auto envWrapper = reinterpret_cast(data); + if (envWrapper == nullptr) { + NETSTACK_LOGE("env wrapper is nullptr"); + return; + } + auto env = *envWrapper; + delete envWrapper; + if (env == nullptr) { + NETSTACK_LOGE("env is nullptr"); + return; + } auto pos = unorderedSetEnv.find(env); if (pos == unorderedSetEnv.end()) { NETSTACK_LOGE("The env is not in the unordered set"); -- Gitee