From b22f786e101a127dd96d5c5b4fd46fe1c4ff10b3 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Mon, 8 Jul 2024 09:54:17 +0800 Subject: [PATCH] add nullptr check function Signed-off-by: yaoruozi1@huawei.com --- .../native/backup_ext/src/ext_extension.cpp | 2 - interfaces/kits/cj/src/file_fileuri_ffi.cpp | 5 +++ .../kits/js/backup/general_callbacks.cpp | 3 ++ .../kits/ndk/fileuri/src/oh_file_uri.cpp | 5 +++ services/backup_sa/src/module_ipc/service.cpp | 43 ++++++++++++++++--- .../src/module_ipc/service_incremental.cpp | 23 +++++++--- .../src/module_sched/sched_scheduler.cpp | 33 ++++++++++---- utils/include/b_resources/b_constants.h | 2 + 8 files changed, 96 insertions(+), 20 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index e8c0e898b..329e08ac3 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -1881,7 +1881,6 @@ std::function BackupExtExtension::HandleTaskBa void BackupExtExtension::WaitToSendFd(std::chrono::system_clock::time_point &startTime, int &fdSendNum) { - HILOGI("WaitToSendFd Begin"); std::unique_lock lock(startSendMutex_); startSendFdRateCon_.wait(lock, [this] { return sendRate_ > 0; }); if (fdSendNum >= sendRate_) { @@ -1899,7 +1898,6 @@ void BackupExtExtension::WaitToSendFd(std::chrono::system_clock::time_point &sta fdSendNum = 0; startTime = std::chrono::system_clock::now(); } - HILOGI("WaitToSendFd End"); } void BackupExtExtension::RefreshTimeInfo(std::chrono::system_clock::time_point &startTime, int &fdSendNum) diff --git a/interfaces/kits/cj/src/file_fileuri_ffi.cpp b/interfaces/kits/cj/src/file_fileuri_ffi.cpp index 58d8d138b..7a2031b92 100644 --- a/interfaces/kits/cj/src/file_fileuri_ffi.cpp +++ b/interfaces/kits/cj/src/file_fileuri_ffi.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include "b_resources/b_constants.h" #include "file_fileuri_ffi.h" #include "file_uri.h" #include "macro.h" @@ -33,6 +34,10 @@ char* MallocCString(const std::string& origin) if (origin.empty()) { return nullptr; } + if (origin.length() > BConstants::PARAM_STRING_MAX_MEMORY) { + LOGE("The param origin is too big"); + return nullptr; + } auto length = origin.length() + 1; char* res = static_cast(malloc(sizeof(char) * length)); if (res == nullptr) { diff --git a/interfaces/kits/js/backup/general_callbacks.cpp b/interfaces/kits/js/backup/general_callbacks.cpp index ed667125e..05fcd6786 100644 --- a/interfaces/kits/js/backup/general_callbacks.cpp +++ b/interfaces/kits/js/backup/general_callbacks.cpp @@ -66,6 +66,9 @@ BackupRestoreCallback::~BackupRestoreCallback() BackupRestoreCallback::operator bool() const { + if (ctx_ == nullptr) { + return false; + } return bool(ctx_->cb_); } diff --git a/interfaces/kits/ndk/fileuri/src/oh_file_uri.cpp b/interfaces/kits/ndk/fileuri/src/oh_file_uri.cpp index b6ffc8843..d2361fdc3 100644 --- a/interfaces/kits/ndk/fileuri/src/oh_file_uri.cpp +++ b/interfaces/kits/ndk/fileuri/src/oh_file_uri.cpp @@ -16,6 +16,7 @@ #include +#include "b_resources/b_constants.h" #include "file_uri.h" #include "log.h" #include "securec.h" @@ -27,6 +28,10 @@ static FileManagement_ErrCode GetValue(std::string_view resultStr, char **result if (count == 0) { return ERR_UNKNOWN; } + if (count > BConstants::PARAM_STRING_MAX_MEMORY) { + LOGE("The param origin is too big"); + return ERR_UNKNOWN; + } *result = static_cast(malloc(sizeof(char) * (count + 1))); if (*result == nullptr) { LOGE("malloc is feiled!"); diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 36d81c7b0..2f06fd4a3 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -591,7 +591,9 @@ ErrCode Service::PublishFile(const BFileInfo &fileInfo) return EPERM; } auto backUpConnection = session_->GetExtConnection(fileInfo.owner); - + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); @@ -664,6 +666,9 @@ ErrCode Service::AppDone(ErrCode errCode) string callerName = VerifyCallerAndGetCallerName(); if (session_->OnBundleFileReady(callerName)) { auto backUpConnection = session_->GetExtConnection(callerName); + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is null"); + } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); @@ -757,6 +762,9 @@ void Service::NotifyCloneBundleFinish(std::string bundleName) { if (session_->OnBundleFileReady(bundleName)) { auto backUpConnection = session_->GetExtConnection(bundleName); + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is null"); + } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); @@ -805,7 +813,9 @@ ErrCode Service::LaunchBackupExtension(const BundleName &bundleName) want.SetParam(BConstants::EXTENSION_BACKUP_EXT_INFO_PARA, bundleExtInfo); auto backUpConnection = session_->GetExtConnection(bundleName); - + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is null"); + } ErrCode ret = backUpConnection->ConnectBackupExtAbility(want, session_->GetSessionUserId()); return ret; } catch (const BError &e) { @@ -852,6 +862,9 @@ ErrCode Service::GetFileHandle(const string &bundleName, const string &fileName) auto action = session_->GetServiceSchedAction(bundleName); if (action == BConstants::ServiceSchedAction::RUNNING) { auto backUpConnection = session_->GetExtConnection(bundleName); + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is null"); + } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); @@ -892,6 +905,10 @@ void Service::OnBackupExtensionDied(const string &&bundleName) // 重新连接清理缓存 HILOGE("Clear backup extension data, bundleName: %{public}s", bundleName.data()); auto backUpConnection = session_->GetExtConnection(bundleName); + if (backUpConnection == nullptr) { + HILOGE("Backup <%{public}s> Extension Process Died, backUpConnection is null", callName.c_str()); + return; + } auto callConnected = [ptr {wptr(this)}](const string &&bundleName) { HILOGE("OnBackupExtensionDied callConnected <%{public}s>", bundleName.c_str()); auto thisPtr = ptr.promote(); @@ -922,6 +939,10 @@ void Service::ExtConnectDied(const string &callName) /* Clear Timer */ session_->BundleExtTimerStop(callName); auto backUpConnection = session_->GetExtConnection(callName); + if (backUpConnection == nullptr) { + HILOGW("Begin, bundleName: %{public}s, backUpConnection is null", callName.c_str()); + return; + } if (backUpConnection->IsExtAbilityConnected()) { backUpConnection->DisconnectBackupExtAbility(); } @@ -951,6 +972,9 @@ void Service::ExtStart(const string &bundleName) } IServiceReverse::Scenario scenario = session_->GetScenario(); auto backUpConnection = session_->GetExtConnection(bundleName); + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is null"); + } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); @@ -977,7 +1001,7 @@ void Service::ExtStart(const string &bundleName) } return; } catch (...) { - HILOGI("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::SA_INVAL_ARG)); return; @@ -987,7 +1011,7 @@ void Service::ExtStart(const string &bundleName) int Service::Dump(int fd, const vector &args) { if (fd < 0) { - HILOGI("HiDumper handle invalid"); + HILOGE("HiDumper handle invalid"); return -1; } @@ -1315,6 +1339,10 @@ ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result) session_->IncreaseSessionCnt(); session_->SetSessionUserId(GetUserIdDefault()); auto backupConnection = session_->CreateBackupConnection(bundleName); + if (backupConnection == nullptr) { + HILOGE("backupConnection is null."); + return BError(BError::Codes::SA_INVAL_ARG); + } auto callConnected = [ptr {wptr(this)}](const string &&bundleName) { HILOGI("callConnected begin."); auto thisPtr = ptr.promote(); @@ -1405,9 +1433,14 @@ ErrCode Service::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool return BError(BError::Codes::SA_INVAL_ARG); } auto backupConnection = session_->GetExtConnection(bundleName); + if (backupConnection == nullptr) { + HILOGE("The backupConnection is null"); + return BError(BError::Codes::SA_INVAL_ARG); + } auto proxy = backupConnection->GetBackupExtProxy(); if (!proxy) { - throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + HILOGE("The extension proxy is null"); + return BError(BError::Codes::SA_INVAL_ARG); } auto ret = proxy->UpdateFdSendRate(bundleName, sendRate); if (ret != NO_ERROR) { diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 88d2a4116..0dc9489a4 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -144,6 +144,9 @@ void Service::StartGetFdTask(std::string bundleName, wptr ptr) throw BError(BError::Codes::SA_INVAL_ARG, "session is nullptr"); } auto backUpConnection = session->GetExtConnection(bundleName); + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is nullptr"); + } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); @@ -327,15 +330,18 @@ ErrCode Service::PublishIncrementalFile(const BFileInfo &fileInfo) return EPERM; } auto backUpConnection = session_->GetExtConnection(fileInfo.owner); + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is nullptr"); + } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); } ErrCode res = proxy->PublishIncrementalFile(fileInfo.fileName); - if (res) { - HILOGE("Failed to publish file for backup extension"); + if (res != ERR_OK) { + HILOGE("Failed to publish file for backup extension, res:%{public}d", res); } - return res; + return ERR_OK; } catch (const BError &e) { return e.GetCode(); } catch (const exception &e) { @@ -471,13 +477,17 @@ ErrCode Service::GetIncrementalFileHandle(const std::string &bundleName, const s auto action = session_->GetServiceSchedAction(bundleName); if (action == BConstants::ServiceSchedAction::RUNNING) { auto backUpConnection = session_->GetExtConnection(bundleName); + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is empty"); + } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); } int res = proxy->GetIncrementalFileHandle(fileName); - if (res) { - HILOGE("Failed to extension file handle"); + if (res != ERR_OK) { + HILOGE("Failed to extension file handle, res: %{public}d", res); + return res; } } else { SvcRestoreDepsManager::GetInstance().UpdateToRestoreBundleMap(bundleName, fileName); @@ -500,6 +510,9 @@ bool Service::IncrementalBackup(const string &bundleName) HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); IServiceReverse::Scenario scenario = session_->GetScenario(); auto backUpConnection = session_->GetExtConnection(bundleName); + if (backUpConnection == nullptr) { + throw BError(BError::Codes::SA_INVAL_ARG, "backUpConnection is empty"); + } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); diff --git a/services/backup_sa/src/module_sched/sched_scheduler.cpp b/services/backup_sa/src/module_sched/sched_scheduler.cpp index be88cf0e2..2b4d2eb0b 100644 --- a/services/backup_sa/src/module_sched/sched_scheduler.cpp +++ b/services/backup_sa/src/module_sched/sched_scheduler.cpp @@ -42,7 +42,12 @@ using namespace std; void SchedScheduler::Sched(string bundleName) { if (bundleName == "") { + if (sessionPtr_ == nullptr) { + HILOGE("Sched bundle %{public}s error, sessionPtr_ is null", bundleName.c_str()); + return; + } if (!sessionPtr_->GetSchedBundleName(bundleName)) { + HILOGE("GetSchedBundleName bundle %{public}s failed", bundleName.c_str()); return; } } @@ -50,6 +55,10 @@ void SchedScheduler::Sched(string bundleName) auto callStart = [schedPtr {wptr(this)}, bundleName]() { try { auto ptr = schedPtr.promote(); + if (ptr == nullptr) { + HILOGE("Sched bundleName %{public}s error, ptr is null", bundleName.data()); + return; + } ptr->ExecutingQueueTasks(bundleName); } catch (const BError &e) { HILOGE("%{public}s", e.what()); @@ -64,7 +73,11 @@ void SchedScheduler::Sched(string bundleName) void SchedScheduler::ExecutingQueueTasks(const string &bundleName) { - HILOGE("start"); + HILOGI("start"); + if (sessionPtr_ == nullptr) { + HILOGE("ExecutingQueueTasks bundle %{public}s error, sessionPtr_ is null", bundleName.c_str()); + return; + } BConstants::ServiceSchedAction action = sessionPtr_->GetServiceSchedAction(bundleName); if (action == BConstants::ServiceSchedAction::START) { // 注册启动定时器 @@ -79,8 +92,11 @@ void SchedScheduler::ExecutingQueueTasks(const string &bundleName) unique_lock lock(lock_); bundleTimeVec_.emplace_back(make_tuple(bundleName, iTime)); lock.unlock(); - // 启动extension - reversePtr_->LaunchBackupExtension(bundleName); + if (reversePtr_ == nullptr) { + HILOGI("Current bundle %{public}s run error, reversePtr_ is null", bundleName.data()); + return; + } + reversePtr_->LaunchBackupExtension(bundleName); // 启动extension } else if (action == BConstants::ServiceSchedAction::RUNNING) { HILOGI("Current bundle %{public}s process is running", bundleName.data()); unique_lock lock(lock_); @@ -92,12 +108,13 @@ void SchedScheduler::ExecutingQueueTasks(const string &bundleName) throw BError(BError::Codes::SA_INVAL_ARG, "Failed to find timer"); } auto &[bName, iTime] = *iter; - // 移除启动定时器 当前逻辑无启动成功后的ext心跳检测 - extTime_.Unregister(iTime); + extTime_.Unregister(iTime); // 移除启动定时器 当前逻辑无启动成功后的ext心跳检测 lock.unlock(); - // 开始执行备份恢复流程 - HILOGI("Current bundle %{public}s extension start", bundleName.data()); - //通知应用市场设置处置 + if (reversePtr_ == nullptr) { + HILOGI("Current bundle %{public}s run error, reversePtr_ is null", bundleName.data()); + return; + } + // 开始执行备份恢复流程,设置处置位 reversePtr_->SendStartAppGalleryNotify(bundleName); reversePtr_->ExtStart(bundleName); } diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index bb1ecccba..e240ed9d3 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -72,6 +72,8 @@ constexpr int BACKUP_VFS_CACHE_PRESSURE = 10000; // 备份过程修改参数 constexpr int32_t INVALID_FD_NUM = -1; +constexpr int32_t PARAM_STRING_MAX_MEMORY = 2 * 1024 * 1024; + constexpr int MAX_FD_SEND_RATE = 800; // 允许应用申请的最大FD数量 constexpr int MIN_FD_SEND_RATE = 0; // 允许应用申请的最小FD数量 constexpr int DEFAULT_FD_SEND_RATE = 60; // 框架默认的FD数量 -- Gitee