diff --git a/interfaces/inner_api/modulemgr/include/module_manager.h b/interfaces/inner_api/modulemgr/include/module_manager.h index d78efbc30da9f39b2a166e2fc7afb6cb1324a2a0..829aaf398d6b1b571dc2aa42d060c5068e9b53d0 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 8436004a3da6790b63f0d61d342d88f499111ebe..ed6f84b728a06911f4b208ea14986b84c889d6a8 100644 --- a/interfaces/inner_api/modulemgr/src/module_manager.cpp +++ b/interfaces/inner_api/modulemgr/src/module_manager.cpp @@ -30,26 +30,34 @@ bool ModuleManager::isLoaded = false; void ModuleManager::LoadModule(std::string libPath) { + 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; + } + free(resolvedPath); 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(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", libPath.c_str()); + UTILS_LOGD("LoadModule so path: %{public}s", dealPath); if (dueModuleHandler == nullptr) { constexpr int32_t maxRetryTimes = 1; int32_t retryTimes = 0; do { - dueModuleHandler = dlopen(libPath.c_str(), RTLD_LAZY); + dueModuleHandler = dlopen(dealPath, 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", dealPath); retryTimes++; if (retryInterval_ > 0 && retryTimes <= maxRetryTimes) { std::this_thread::sleep_for(std::chrono::milliseconds(retryInterval_)); @@ -69,9 +77,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 +111,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 +133,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 129c488d3479619976c318bfabdcb80869128ef9..9ab6ac6ec973115d678093bee380a3c49d4f1e51 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 afe8a01cef8948b8afa5e166a906334aa29c7955..eae24d5ddf73e0d11dc23ab4d52f6eefe89c4470 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 76d94322de1ecd400d11b9af9368477c4c1be511..08104640c0da78808d3aafeac38c73def1a02cd1 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 false; + } + 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 ae188fe79469fdaa9e9b912a721bff5b439fc845..54b6855265f52660b39e2453914297dcf4d85b16 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;