From 2f72417558fe73dab186836c07ea2ba027d6136d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Thu, 14 Mar 2024 21:36:22 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9codex=20Signed-off-by:=20?= =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modulemgr/include/module_manager.h | 1 + .../modulemgr/src/module_manager.cpp | 12 +++++- napi/include/napi_base.h | 17 ++++++-- .../preference/src/preference_utils.cpp | 39 +++++++++++++++++++ .../core/ability/utils/src/file_utils.cpp | 18 ++++++++- .../firmware/check/include/firmware_icheck.h | 1 + 6 files changed, 81 insertions(+), 7 deletions(-) diff --git a/interfaces/inner_api/modulemgr/include/module_manager.h b/interfaces/inner_api/modulemgr/include/module_manager.h index d78efbc3..829aaf39 100644 --- a/interfaces/inner_api/modulemgr/include/module_manager.h +++ b/interfaces/inner_api/modulemgr/include/module_manager.h @@ -53,6 +53,7 @@ private: static std::map onStartOnStopFuncMap_; static std::map onIdleFuncMap_; static bool isLoaded; + std::mutex mapMutex_; }; } // namespace UpdateEngine } // namespace OHOS diff --git a/interfaces/inner_api/modulemgr/src/module_manager.cpp b/interfaces/inner_api/modulemgr/src/module_manager.cpp index 8436004a..09a230f1 100644 --- a/interfaces/inner_api/modulemgr/src/module_manager.cpp +++ b/interfaces/inner_api/modulemgr/src/module_manager.cpp @@ -30,6 +30,13 @@ bool ModuleManager::isLoaded = false; void ModuleManager::LoadModule(std::string libPath) { + char realPath[PATH_MAX] = {0}; + char *resolvedPath = realpath(libPath.c_str(), realPath); + if (resolvedPath == NULL) { + ENGINE_LOGI("%s is not exist or invalid", libPath.c_str()); + return; + } + free(resolvedPath); std::string prefix = "/system/lib64/updateext"; std::string modulePrefix = "/module_update/3006/lib64/updateext"; std::string suffix = ".so"; @@ -69,9 +76,10 @@ ModuleManager& ModuleManager::GetInstance() void ModuleManager::HookFunc(std::vector codes, RequestFuncType handleRemoteRequest) { - for (int code : codes) { + for (const uint32_t code : codes) { if (onRemoteRequestFuncMap_.find(code) == onRemoteRequestFuncMap_.end()) { UTILS_LOGE("code not exist %{public}d onRemoteRequestFuncMap_", code); + std::unique_lock lock(mapMutex_); onRemoteRequestFuncMap_.insert(std::make_pair(code, handleRemoteRequest)); } else { UTILS_LOGD("add code %{public}d to onRemoteRequestFuncMap_", code); @@ -102,6 +110,7 @@ void ModuleManager::HookOnStartOnStopFunc(std::string phase, LifeCycleFuncType h { if (onStartOnStopFuncMap_.find(phase) == onStartOnStopFuncMap_.end()) { UTILS_LOGE("phase exist already %{public}s onStartOnStopFuncMap_", phase.c_str()); + std::unique_lock lock(mapMutex_); onStartOnStopFuncMap_.insert(std::make_pair(phase, handleSAOnStartOnStop)); } else { UTILS_LOGD("add phase %{public}s to onStartOnStopFuncMap_", phase.c_str()); @@ -123,6 +132,7 @@ void ModuleManager::HookOnIdleFunc(std::string phase, LifeCycleFuncReturnType ha { if (onIdleFuncMap_.find(phase) == onIdleFuncMap_.end()) { UTILS_LOGE("phase exist already %{public}s onIdleFuncMap_", phase.c_str()); + std::unique_lock lock(mapMutex_); onIdleFuncMap_.insert(std::make_pair(phase, handleSAOnIdle)); } else { UTILS_LOGD("add phase %{public}s to onIdleFuncMap_", phase.c_str()); diff --git a/napi/include/napi_base.h b/napi/include/napi_base.h index 129c488d..9ab6ac6e 100644 --- a/napi/include/napi_base.h +++ b/napi/include/napi_base.h @@ -113,17 +113,22 @@ public: napi_value result[] = { nullptr, nullptr }; bool isSuccess = BuildResult(env, clientContext, finalResult, result); if (clientContext->deferred_) { // promise调用 - ExecutePromiseFunc(env, clientContext, result, isSuccess); + ExecutePromiseFunc(env, clientContext, result, isSuccess, sizeof(result)); } else { - ExecuteCallbackFunc(env, clientContext, result); + ExecuteCallbackFunc(env, clientContext, result, sizeof(result)); } napi_delete_async_work(env, clientContext->work_); delete clientContext; // Execute 中释放控制权的指针,在此处释放 clientContext = nullptr; } - static void ExecutePromiseFunc(napi_env env, T *clientContext, napi_value const * result, bool isSuccess) + static void ExecutePromiseFunc(napi_env env, T *clientContext, napi_value const * result, bool isSuccess, + size_t len) { + if (len <= 0) { + ENGINE_LOGE("result is null"); + return; + } napi_status callbackStatus = isSuccess ? napi_resolve_deferred(env, clientContext->deferred_, result[1]) : napi_reject_deferred(env, clientContext->deferred_, result[0]); if (callbackStatus != napi_ok) { @@ -131,8 +136,12 @@ public: } } - static void ExecuteCallbackFunc(napi_env env, T *clientContext, napi_value *result) + static void ExecuteCallbackFunc(napi_env env, T *clientContext, napi_value *result, size_t len) { + if (len <= 0) { + ENGINE_LOGE("result is null"); + return; + } napi_value callback = nullptr; napi_status resultStatus = napi_get_reference_value(env, clientContext->callbackRef_, &callback); if (resultStatus != napi_ok) { diff --git a/services/core/ability/preference/src/preference_utils.cpp b/services/core/ability/preference/src/preference_utils.cpp index afe8a01c..eae24d5d 100644 --- a/services/core/ability/preference/src/preference_utils.cpp +++ b/services/core/ability/preference/src/preference_utils.cpp @@ -88,30 +88,49 @@ bool PreferencesUtil::Save(const std::string &key, const T &value) bool PreferencesUtil::SaveInner( std::shared_ptr ptr, const std::string &key, const std::string &value) { + if (ptr == nullptr) { + return false; + } return ptr->PutString(key, value) == NativePreferences::E_OK; } bool PreferencesUtil::SaveInner( std::shared_ptr ptr, const std::string &key, const int &value) { + if (ptr == nullptr) { + ENGINE_LOGE("ptr is null"); + return false; + } return ptr->PutInt(key, value) == NativePreferences::E_OK; } bool PreferencesUtil::SaveInner( std::shared_ptr ptr, const std::string &key, const bool &value) { + if (ptr == nullptr) { + ENGINE_LOGE("ptr is null"); + return false; + } return ptr->PutBool(key, value) == NativePreferences::E_OK; } bool PreferencesUtil::SaveInner( std::shared_ptr ptr, const std::string &key, const int64_t &value) { + if (ptr == nullptr) { + ENGINE_LOGE("ptr is null"); + return false; + } return ptr->PutLong(key, value) == NativePreferences::E_OK; } bool PreferencesUtil::SaveInner( std::shared_ptr ptr, const std::string &key, const float &value) { + if (ptr == nullptr) { + ENGINE_LOGE("ptr is null"); + return false; + } return ptr->PutFloat(key, value) == NativePreferences::E_OK; } @@ -155,30 +174,50 @@ T PreferencesUtil::Obtain(const std::string &key, const T &defValue) std::string PreferencesUtil::ObtainInner( std::shared_ptr ptr, const std::string &key, const std::string &defValue) { + if (ptr == nullptr) { + ENGINE_LOGE("ptr is null"); + return defValue; + } return ptr->GetString(key, defValue); } int PreferencesUtil::ObtainInner( std::shared_ptr ptr, const std::string &key, const int &defValue) { + if (ptr == nullptr) { + ENGINE_LOGE("ptr is null"); + return defValue; + } return ptr->GetInt(key, defValue); } bool PreferencesUtil::ObtainInner( std::shared_ptr ptr, const std::string &key, const bool &defValue) { + if (ptr == nullptr) { + ENGINE_LOGE("ptr is null"); + return defValue; + } return ptr->GetBool(key, defValue); } int64_t PreferencesUtil::ObtainInner( std::shared_ptr ptr, const std::string &key, const int64_t &defValue) { + if (ptr == nullptr) { + ENGINE_LOGE("ptr is null"); + return defValue; + } return ptr->GetLong(key, defValue); } float PreferencesUtil::ObtainInner( std::shared_ptr ptr, const std::string &key, const float &defValue) { + if (ptr == nullptr) { + ENGINE_LOGE("ptr is null"); + return defValue; + } return ptr->GetFloat(key, defValue); } diff --git a/services/core/ability/utils/src/file_utils.cpp b/services/core/ability/utils/src/file_utils.cpp index 76d94322..10f0c3fe 100644 --- a/services/core/ability/utils/src/file_utils.cpp +++ b/services/core/ability/utils/src/file_utils.cpp @@ -67,7 +67,14 @@ bool FileUtils::IsSpaceEnough(const std::string &filePath, const int64_t require bool FileUtils::SaveDataToFile(const std::string &filePath, const std::string &data) { std::ofstream os; - os.open(filePath, std::ios::trunc); + char realPath[PATH_MAX] = {0}; + char *resolvedPath = realpath(filePath.c_str(), realPath); + if (resolvedPath == NULL) { + ENGINE_LOGI("%s is not exist or invalid", filePath.c_str()); + return; + } + free(resolvedPath); + os.open(realPath, std::ios::trunc); if (os.is_open()) { ENGINE_LOGI("SaveDataToFile success, file = %{public}s", filePath.c_str()); os << data; @@ -203,7 +210,14 @@ bool FileUtils::CreatDirWithPermission(const std::string &fileDir, int32_t dirPe std::string FileUtils::ReadDataFromFile(const std::string &filePath) { std::ifstream readFile; - readFile.open(filePath); + char realPath[PATH_MAX] = {0}; + char *resolvedPath = realpath(filePath.c_str(), realPath); + if (resolvedPath == NULL) { + ENGINE_LOGI("%s is not exist or invalid", filePath.c_str()); + return; + } + free(resolvedPath); + readFile.open(realPath); if (readFile.fail()) { ENGINE_LOGI("open file from %{public}s err", filePath.c_str()); return ""; diff --git a/services/firmware/check/include/firmware_icheck.h b/services/firmware/check/include/firmware_icheck.h index ae188fe7..54b68552 100644 --- a/services/firmware/check/include/firmware_icheck.h +++ b/services/firmware/check/include/firmware_icheck.h @@ -123,6 +123,7 @@ private: cJSON *item = cJSON_GetObjectItem(root, "searchStatus"); ENGINE_CHECK(item != nullptr, cJSON_Delete(root); return -1, "Error get searchStatus"); + cJSON_Delete(root); if (!cJSON_IsNumber(item)) { FIRMWARE_LOGE("Error json parse"); return -1; -- Gitee From dc1c354b342751e50fb7be6c707c35081ae327ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Thu, 14 Mar 2024 13:58:57 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9RealPath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- .../inner_api/modulemgr/src/module_manager.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/interfaces/inner_api/modulemgr/src/module_manager.cpp b/interfaces/inner_api/modulemgr/src/module_manager.cpp index 09a230f1..2970875b 100644 --- a/interfaces/inner_api/modulemgr/src/module_manager.cpp +++ b/interfaces/inner_api/modulemgr/src/module_manager.cpp @@ -40,23 +40,24 @@ void ModuleManager::LoadModule(std::string libPath) std::string prefix = "/system/lib64/updateext"; std::string modulePrefix = "/module_update/3006/lib64/updateext"; std::string suffix = ".so"; - if ((libPath.substr(0, prefix.length()) != prefix && - libPath.substr(0, modulePrefix.length()) != modulePrefix) || - (libPath.substr(libPath.length() - suffix.length(), suffix.length()) != suffix)) { + std::string middlePath = std::to_string(realPath); + if ((middlePath.substr(0, prefix.length()) != prefix && + middlePath.substr(0, modulePrefix.length()) != modulePrefix) || + (middlePath.substr(middlePath.length() - suffix.length(), suffix.length()) != suffix)) { UTILS_LOGE("LoadModule lib path invalid"); return; } - UTILS_LOGD("LoadModule so path: %{public}s", libPath.c_str()); + UTILS_LOGD("LoadModule so path: %{public}s", realPath); if (dueModuleHandler == nullptr) { constexpr int32_t maxRetryTimes = 1; int32_t retryTimes = 0; do { - dueModuleHandler = dlopen(libPath.c_str(), RTLD_LAZY); + dueModuleHandler = dlopen(realPath, RTLD_LAZY); if (dueModuleHandler != nullptr) { isLoaded = true; break; } - UTILS_LOGE("openSo path: %{public}s fail", libPath.c_str()); + UTILS_LOGE("openSo path: %{public}s fail", realPath); retryTimes++; if (retryInterval_ > 0 && retryTimes <= maxRetryTimes) { std::this_thread::sleep_for(std::chrono::milliseconds(retryInterval_)); -- Gitee From 08adfb78b2d2dd13f425452549fcbfe7fc765d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Thu, 14 Mar 2024 14:05:58 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9realpath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- interfaces/inner_api/modulemgr/src/module_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/inner_api/modulemgr/src/module_manager.cpp b/interfaces/inner_api/modulemgr/src/module_manager.cpp index 2970875b..94b4d19f 100644 --- a/interfaces/inner_api/modulemgr/src/module_manager.cpp +++ b/interfaces/inner_api/modulemgr/src/module_manager.cpp @@ -33,7 +33,7 @@ void ModuleManager::LoadModule(std::string libPath) char realPath[PATH_MAX] = {0}; char *resolvedPath = realpath(libPath.c_str(), realPath); if (resolvedPath == NULL) { - ENGINE_LOGI("%s is not exist or invalid", libPath.c_str()); + UTILS_LOGE("%s is not exist or invalid", libPath.c_str()); return; } free(resolvedPath); -- Gitee From 0f5504d9d41750662b8c8dce238dbf33d40186c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Fri, 15 Mar 2024 00:34:08 +0000 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9codex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- interfaces/inner_api/modulemgr/src/module_manager.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interfaces/inner_api/modulemgr/src/module_manager.cpp b/interfaces/inner_api/modulemgr/src/module_manager.cpp index 94b4d19f..13c723d8 100644 --- a/interfaces/inner_api/modulemgr/src/module_manager.cpp +++ b/interfaces/inner_api/modulemgr/src/module_manager.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include "../include/module_log.h" -- Gitee From e6e1cbb585cf20420c9b8d1757d9a639cbe595ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Fri, 15 Mar 2024 01:03:36 +0000 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9codex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- interfaces/inner_api/modulemgr/src/module_manager.cpp | 3 +-- services/core/ability/utils/src/file_utils.cpp | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/interfaces/inner_api/modulemgr/src/module_manager.cpp b/interfaces/inner_api/modulemgr/src/module_manager.cpp index 13c723d8..1dc5253e 100644 --- a/interfaces/inner_api/modulemgr/src/module_manager.cpp +++ b/interfaces/inner_api/modulemgr/src/module_manager.cpp @@ -15,7 +15,6 @@ #include #include -#include #include #include "../include/module_log.h" @@ -41,7 +40,7 @@ void ModuleManager::LoadModule(std::string libPath) std::string prefix = "/system/lib64/updateext"; std::string modulePrefix = "/module_update/3006/lib64/updateext"; std::string suffix = ".so"; - std::string middlePath = std::to_string(realPath); + std::string middlePath(realPath); if ((middlePath.substr(0, prefix.length()) != prefix && middlePath.substr(0, modulePrefix.length()) != modulePrefix) || (middlePath.substr(middlePath.length() - suffix.length(), suffix.length()) != suffix)) { diff --git a/services/core/ability/utils/src/file_utils.cpp b/services/core/ability/utils/src/file_utils.cpp index 10f0c3fe..08104640 100644 --- a/services/core/ability/utils/src/file_utils.cpp +++ b/services/core/ability/utils/src/file_utils.cpp @@ -71,7 +71,7 @@ bool FileUtils::SaveDataToFile(const std::string &filePath, const std::string &d char *resolvedPath = realpath(filePath.c_str(), realPath); if (resolvedPath == NULL) { ENGINE_LOGI("%s is not exist or invalid", filePath.c_str()); - return; + return false; } free(resolvedPath); os.open(realPath, std::ios::trunc); @@ -214,7 +214,7 @@ std::string FileUtils::ReadDataFromFile(const std::string &filePath) char *resolvedPath = realpath(filePath.c_str(), realPath); if (resolvedPath == NULL) { ENGINE_LOGI("%s is not exist or invalid", filePath.c_str()); - return; + return ""; } free(resolvedPath); readFile.open(realPath); -- Gitee From 9be37a79534d7cf2867b905e229bc8d87672be75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Fri, 15 Mar 2024 02:06:44 +0000 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9codex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- interfaces/inner_api/modulemgr/src/module_manager.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/interfaces/inner_api/modulemgr/src/module_manager.cpp b/interfaces/inner_api/modulemgr/src/module_manager.cpp index 1dc5253e..89c18e54 100644 --- a/interfaces/inner_api/modulemgr/src/module_manager.cpp +++ b/interfaces/inner_api/modulemgr/src/module_manager.cpp @@ -30,8 +30,8 @@ bool ModuleManager::isLoaded = false; void ModuleManager::LoadModule(std::string libPath) { - char realPath[PATH_MAX] = {0}; - char *resolvedPath = realpath(libPath.c_str(), realPath); + char dealPath[PATH_MAX] = {0}; + char *resolvedPath = realpath(libPath.c_str(), dealPath); if (resolvedPath == NULL) { UTILS_LOGE("%s is not exist or invalid", libPath.c_str()); return; @@ -40,14 +40,14 @@ void ModuleManager::LoadModule(std::string libPath) std::string prefix = "/system/lib64/updateext"; std::string modulePrefix = "/module_update/3006/lib64/updateext"; std::string suffix = ".so"; - std::string middlePath(realPath); + std::string middlePath(dealPath); if ((middlePath.substr(0, prefix.length()) != prefix && middlePath.substr(0, modulePrefix.length()) != modulePrefix) || (middlePath.substr(middlePath.length() - suffix.length(), suffix.length()) != suffix)) { UTILS_LOGE("LoadModule lib path invalid"); return; } - UTILS_LOGD("LoadModule so path: %{public}s", realPath); + UTILS_LOGD("LoadModule so path: %{public}s", dealPath); if (dueModuleHandler == nullptr) { constexpr int32_t maxRetryTimes = 1; int32_t retryTimes = 0; @@ -57,7 +57,7 @@ void ModuleManager::LoadModule(std::string libPath) isLoaded = true; break; } - UTILS_LOGE("openSo path: %{public}s fail", realPath); + UTILS_LOGE("openSo path: %{public}s fail", dealPath); retryTimes++; if (retryInterval_ > 0 && retryTimes <= maxRetryTimes) { std::this_thread::sleep_for(std::chrono::milliseconds(retryInterval_)); -- Gitee From 30f1adcbdc33120b433635842dddd610f7191b04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Fri, 15 Mar 2024 02:41:38 +0000 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9codex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- interfaces/inner_api/modulemgr/src/module_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/inner_api/modulemgr/src/module_manager.cpp b/interfaces/inner_api/modulemgr/src/module_manager.cpp index 89c18e54..ed6f84b7 100644 --- a/interfaces/inner_api/modulemgr/src/module_manager.cpp +++ b/interfaces/inner_api/modulemgr/src/module_manager.cpp @@ -52,7 +52,7 @@ void ModuleManager::LoadModule(std::string libPath) constexpr int32_t maxRetryTimes = 1; int32_t retryTimes = 0; do { - dueModuleHandler = dlopen(realPath, RTLD_LAZY); + dueModuleHandler = dlopen(dealPath, RTLD_LAZY); if (dueModuleHandler != nullptr) { isLoaded = true; break; -- Gitee