已关闭
物理路径转沙箱路径优化适配 #20367
dgm_2399562创建于 7 天前关闭于 6 天前
物理路径转沙箱路径优化适配 #20367
已关闭
dgm_2399562创建于 7 天前关闭于 6 天前
已删除 :master合入到openharmony/ability_ability_runtimemaster
8 个文件变更+33-44
@@ -66,8 +66,6 @@ constexpr char SANDBOX_ARK_PROFILE_PATH[] = "/data/storage/ark-profile/";
66constexpr char ARK_PROFILE_SUFFIX[] = ".ap";66constexpr char ARK_PROFILE_SUFFIX[] = ".ap";
67constexpr char MERGE_ABC_PATH[] = "/ets/modules_static.abc";67constexpr char MERGE_ABC_PATH[] = "/ets/modules_static.abc";
68constexpr char ABS_DATA_CODE_PATH[] = "/data/app/el1/bundle/public/";68constexpr char ABS_DATA_CODE_PATH[] = "/data/app/el1/bundle/public/";
69-constexpr char BUNDLE[] = "bundle/";
70-constexpr char ABS_CODE_PATH[] = "/data/storage/el1/";
71 69 
72 70 
73using CreateVMETSRuntimeType = ani_status (*)(const ani_options *options, uint32_t version, ani_vm **result);71using CreateVMETSRuntimeType = ani_status (*)(const ani_options *options, uint32_t version, ani_vm **result);
@@ -1000,8 +998,9 @@ std::vector<std::string> ETSEnvironment::GetHspPathList()
1000 998 
1001 for (const auto &pluginHspPath : staticPluginHspPathList_) {999 for (const auto &pluginHspPath : staticPluginHspPathList_) {
1002 std::string targetPath = pluginHspPath;1000 std::string targetPath = pluginHspPath;
1003- std::regex patter(std::string(ABS_DATA_CODE_PATH) + bundleName_ + "/");1001+ std::regex patter(AbilityRuntime::Runtime::GetSendboxPath());
A
Aafwk_helper7 天前

🤖 AI 代码检视意见(回复本评论可解决检视意见,点击被检视代码行左侧的小头像可收起检视意见)


🟠 substr(find('/')+1) 未处理 npos 且引用疑似拼写错误的 GetSendboxPath

位置: L1001-L1003 | 严重程度: High

❓ 问题描述

新代码使用 targetPath.find("/")+1 取子串,若 find 返回 npos,则 npos+1 回绕为 0,结果为整串,行为不明确;同时 AbilityRuntime::Runtime::GetSendboxPath() 看起来是 Sandbox 的拼写错误,若 API 命名确为如此则可忽略,否则编译会失败或调用错误方法。此外变量名 patter 仍保留拼写错误(应为 pattern),代码风格上还使用了 Tab 缩进与项目其他位置不一致。

💡 修复建议

修改建议:显式判断 find 结果是否为 npos;确认并使用正确的 Sandbox API 名称;将变量名 patter 改为 pattern,并将 Tab 替换为 4 空格缩进。

1001: std::regex pattern(AbilityRuntime::Runtime::GetSandboxPath()); 1002: targetPath = std::regex_replace(targetPath, pattern, ""); 1003: auto pos = targetPath.find("/"); 1004: targetPath = (pos == std::string::npos) ? (std::string(BUNDLE_INSTALL_PATH) + targetPath) 1005: : (std::string(BUNDLE_INSTALL_PATH) + targetPath.substr(pos + 1));


likedislike
1004- targetPath = std::regex_replace(targetPath, patter, std::string(ABS_CODE_PATH) + std::string(BUNDLE));1002+ targetPath = std::regex_replace(targetPath, patter, "");
1003+ targetPath = std::string(BUNDLE_INSTALL_PATH) + targetPath.substr(targetPath.find("/") + 1);
1005 hspPathList.push_back(targetPath);1004 hspPathList.push_back(targetPath);
1006 }1005 }
1007 1006 
@@ -60,7 +60,7 @@ std::mutex ContextImpl::getAllUIAbilitiesCallbackMutex_;
60GetAllUIAbilitiesCallback ContextImpl::getAllUIAbilitiesCallback_ = nullptr;60GetAllUIAbilitiesCallback ContextImpl::getAllUIAbilitiesCallback_ = nullptr;
61#endif61#endif
62using namespace OHOS::AbilityBase::Constants;62using namespace OHOS::AbilityBase::Constants;
63- 63+using ExtractorUtil = AbilityBase::ExtractorUtil;
64const std::string PATTERN_VERSION = std::string(FILE_SEPARATOR) + "v\\d+" + FILE_SEPARATOR;64const std::string PATTERN_VERSION = std::string(FILE_SEPARATOR) + "v\\d+" + FILE_SEPARATOR;
65 65 
66const size_t Context::CONTEXT_TYPE_ID(std::hash<const char*> {} ("Context"));66const size_t Context::CONTEXT_TYPE_ID(std::hash<const char*> {} ("Context"));
@@ -542,9 +542,7 @@ std::shared_ptr<Context> ContextImpl::WrapContext(const std::string &pluginBundl
542 continue;542 continue;
543 }543 }
544 if (inputContext->GetBundleName() == hostBundleName) {544 if (inputContext->GetBundleName() == hostBundleName) {
545- std::regex pattern(545+ loadPath = ExtractorUtil::GetLoadFilePath(loadPath);
546- std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + inputContext->GetBundleName());
547- loadPath = std::regex_replace(loadPath, pattern, LOCAL_CODE_PATH);
548 bool newCreate = false;546 bool newCreate = false;
549 std::shared_ptr<AbilityBase::Extractor> extractor =547 std::shared_ptr<AbilityBase::Extractor> extractor =
550 AbilityBase::ExtractorUtil::GetExtractor(loadPath, newCreate, true);548 AbilityBase::ExtractorUtil::GetExtractor(loadPath, newCreate, true);
@@ -991,8 +989,7 @@ void ContextImpl::InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo,
991 TAG_LOGE(AAFwkTag::APPKIT, "empty loadPath");989 TAG_LOGE(AAFwkTag::APPKIT, "empty loadPath");
992 break;990 break;
993 }991 }
994- std::regex pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + bundleInfo.name);992+ loadPath = ExtractorUtil::GetLoadFilePath(loadPath);
995- loadPath = std::regex_replace(loadPath, pattern, std::string(LOCAL_CODE_PATH));
996 if (!resourceManager->AddResource(loadPath.c_str())) {993 if (!resourceManager->AddResource(loadPath.c_str())) {
997 TAG_LOGE(AAFwkTag::APPKIT, "AddResource failed");994 TAG_LOGE(AAFwkTag::APPKIT, "AddResource failed");
998 }995 }
@@ -1046,8 +1043,6 @@ std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::InitResourceMana
1046 }1043 }
1047 if (!moduleName.empty() || !bundleInfo.applicationInfo.multiProjects) {1044 if (!moduleName.empty() || !bundleInfo.applicationInfo.multiProjects) {
1048 TAG_LOGD(AAFwkTag::APPKIT, "hapModuleInfos count: %{public}zu", bundleInfo.hapModuleInfos.size());1045 TAG_LOGD(AAFwkTag::APPKIT, "hapModuleInfos count: %{public}zu", bundleInfo.hapModuleInfos.size());
1049- std::regex inner_pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR)
1050- + GetBundleNameWithContext(inputContext));
1051 std::regex outer_pattern(ABS_CODE_PATH);1046 std::regex outer_pattern(ABS_CODE_PATH);
1052 std::regex hsp_pattern(std::string(ABS_CODE_PATH) + FILE_SEPARATOR + bundleInfo.name + PATTERN_VERSION);1047 std::regex hsp_pattern(std::string(ABS_CODE_PATH) + FILE_SEPARATOR + bundleInfo.name + PATTERN_VERSION);
1053 std::string hsp_sandbox = std::string(LOCAL_CODE_PATH) + FILE_SEPARATOR + bundleInfo.name + FILE_SEPARATOR;1048 std::string hsp_sandbox = std::string(LOCAL_CODE_PATH) + FILE_SEPARATOR + bundleInfo.name + FILE_SEPARATOR;
@@ -1069,7 +1064,7 @@ std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::InitResourceMana
1069 *deduplicate |= hapModuleInfo.deduplicateHar;1064 *deduplicate |= hapModuleInfo.deduplicateHar;
1070 }1065 }
1071 if (currentBundle) {1066 if (currentBundle) {
1072- loadPath = std::regex_replace(loadPath, inner_pattern, LOCAL_CODE_PATH);1067+ loadPath = ExtractorUtil::GetLoadFilePath(loadPath);;
1073 } else if (bundleInfo.applicationInfo.bundleType == AppExecFwk::BundleType::SHARED) {1068 } else if (bundleInfo.applicationInfo.bundleType == AppExecFwk::BundleType::SHARED) {
1074 loadPath = std::regex_replace(loadPath, hsp_pattern, hsp_sandbox);1069 loadPath = std::regex_replace(loadPath, hsp_pattern, hsp_sandbox);
1075 } else if (bundleInfo.applicationInfo.bundleType == AppExecFwk::BundleType::APP_SERVICE_FWK) {1070 } else if (bundleInfo.applicationInfo.bundleType == AppExecFwk::BundleType::APP_SERVICE_FWK) {
@@ -1092,10 +1087,8 @@ std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::InitResourceMana
1092void ContextImpl::AddPatchResource(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,1087void ContextImpl::AddPatchResource(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
1093 const std::string &loadPath, const std::string &hqfPath, bool isDebug, std::shared_ptr<Context> inputContext)1088 const std::string &loadPath, const std::string &hqfPath, bool isDebug, std::shared_ptr<Context> inputContext)
1094{1089{
1095- std::regex pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR)
1096- + GetBundleNameWithContext(inputContext));
1097 if (!hqfPath.empty() && isDebug) {1090 if (!hqfPath.empty() && isDebug) {
1098- std::string realHqfPath = std::regex_replace(hqfPath, pattern, LOCAL_CODE_PATH);1091+ std::string realHqfPath = ExtractorUtil::GetLoadFilePath(hqfPath);
1099 TAG_LOGI(AAFwkTag::APPKIT, "AddPatchResource hapPath:%{public}s, patchPath:%{public}s",1092 TAG_LOGI(AAFwkTag::APPKIT, "AddPatchResource hapPath:%{public}s, patchPath:%{public}s",
1100 loadPath.c_str(), realHqfPath.c_str());1093 loadPath.c_str(), realHqfPath.c_str());
1101 if (!resourceManager->AddPatchResource(loadPath.c_str(), realHqfPath.c_str())) {1094 if (!resourceManager->AddPatchResource(loadPath.c_str(), realHqfPath.c_str())) {
@@ -1128,8 +1121,7 @@ void ContextImpl::GetOverlayPath(std::shared_ptr<Global::Resource::ResourceManag
1128 TAG_LOGE(AAFwkTag::APPKIT, "hapPath: %{private}s", it.hapPath.c_str());1121 TAG_LOGE(AAFwkTag::APPKIT, "hapPath: %{private}s", it.hapPath.c_str());
1129 }1122 }
1130 if (isMatched) {1123 if (isMatched) {
1131- it.hapPath = std::regex_replace(it.hapPath, std::regex(std::string(ABS_CODE_PATH) +1124+ it.hapPath = ExtractorUtil::GetLoadFilePath(it.hapPath);
1132- std::string(FILE_SEPARATOR) + GetBundleNameWithContext(inputContext)), LOCAL_CODE_PATH);
1133 } else {1125 } else {
1134 it.hapPath = std::regex_replace(it.hapPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);1126 it.hapPath = std::regex_replace(it.hapPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
1135 }1127 }
@@ -1710,7 +1702,6 @@ void ContextImpl::OnOverlayChanged(const EventFwk::CommonEventData &data,
1710 1702 
1711void ContextImpl::ChangeToLocalPath(const std::string& bundleName, const std::string& sourceDir, std::string& localPath)1703void ContextImpl::ChangeToLocalPath(const std::string& bundleName, const std::string& sourceDir, std::string& localPath)
1712{1704{
1713- std::regex pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + bundleName);
1714 if (sourceDir.empty()) {1705 if (sourceDir.empty()) {
1715 return;1706 return;
1716 }1707 }
@@ -1722,7 +1713,7 @@ void ContextImpl::ChangeToLocalPath(const std::string& bundleName, const std::st
1722 localPath.c_str(), bundleName.c_str());1713 localPath.c_str(), bundleName.c_str());
1723 }1714 }
1724 if (isExist) {1715 if (isExist) {
1725- localPath = std::regex_replace(localPath, pattern, std::string(LOCAL_CODE_PATH));1716+ localPath = ExtractorUtil::GetLoadFilePath(localPath);
1726 } else {1717 } else {
1727 localPath = std::regex_replace(localPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);1718 localPath = std::regex_replace(localPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
1728 }1719 }
@@ -29,12 +29,13 @@
29#include "os_account_manager_wrapper.h"29#include "os_account_manager_wrapper.h"
30#include "sys_mgr_client.h"30#include "sys_mgr_client.h"
31#include "system_ability_definition.h"31#include "system_ability_definition.h"
32+#include "extractor.h"
32 33 
33#define MODE 077134#define MODE 0771
34namespace OHOS {35namespace OHOS {
35namespace AppExecFwk {36namespace AppExecFwk {
36using namespace OHOS::AbilityBase::Constants;37using namespace OHOS::AbilityBase::Constants;
37- 38+using ExtractorUtil = AbilityBase::ExtractorUtil;
38const std::string ContextDeal::CONTEXT_DEAL_FILE_SEPARATOR("/");39const std::string ContextDeal::CONTEXT_DEAL_FILE_SEPARATOR("/");
39const std::string ContextDeal::CONTEXT_DEAL_Files("files");40const std::string ContextDeal::CONTEXT_DEAL_Files("files");
40const int64_t ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001);41const int64_t ContextDeal::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001);
@@ -200,8 +201,7 @@ std::string ContextDeal::GetBundleResourcePath()
200 if (isCreateBySystemApp_) {201 if (isCreateBySystemApp_) {
201 dir = std::regex_replace(abilityInfo_->resourcePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);202 dir = std::regex_replace(abilityInfo_->resourcePath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
202 } else {203 } else {
203- std::regex pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + abilityInfo_->bundleName);204+ dir = ExtractorUtil::GetLoadFilePath(abilityInfo_->resourcePath);
204- dir = std::regex_replace(abilityInfo_->resourcePath, pattern, LOCAL_CODE_PATH);
205 }205 }
206 return dir;206 return dir;
207}207}
@@ -112,6 +112,7 @@
112#endif112#endif
113 113 
114#include "sleep_clean.h"114#include "sleep_clean.h"
115+#include "extractor.h"
115 116 
116#if defined(NWEB)117#if defined(NWEB)
117#include <thread>118#include <thread>
@@ -149,7 +150,7 @@ constexpr char FORM_RENDER_LIB_PATH[] = "/system/lib64/libformrender.z.so";
149#else150#else
150constexpr char FORM_RENDER_LIB_PATH[] = "/system/lib64/libformrender.z.so";151constexpr char FORM_RENDER_LIB_PATH[] = "/system/lib64/libformrender.z.so";
151#endif152#endif
152- 153+using ExtractorUtil = AbilityBase::ExtractorUtil;
153constexpr int32_t DELIVERY_TIME = 200;154constexpr int32_t DELIVERY_TIME = 200;
154constexpr int32_t DISTRIBUTE_TIME = 100;155constexpr int32_t DISTRIBUTE_TIME = 100;
155constexpr int32_t START_HIGH_SENSITIVE = 1;156constexpr int32_t START_HIGH_SENSITIVE = 1;
@@ -1327,8 +1328,7 @@ std::vector<std::string> MainThread::GetOverlayPaths(const std::string &bundleNa
1327 std::vector<std::string> overlayPaths;1328 std::vector<std::string> overlayPaths;
1328 for (auto &it : overlayModuleInfos_) {1329 for (auto &it : overlayModuleInfos_) {
1329 if (std::regex_search(it.hapPath, std::regex(bundleName))) {1330 if (std::regex_search(it.hapPath, std::regex(bundleName))) {
1330- it.hapPath = std::regex_replace(it.hapPath, std::regex(std::string(ABS_CODE_PATH) +1331+ it.hapPath = ExtractorUtil::GetLoadFilePath(it.hapPath);
1331- std::string(FILE_SEPARATOR) + bundleName), std::string(LOCAL_CODE_PATH));
1332 } else {1332 } else {
1333 it.hapPath = std::regex_replace(it.hapPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);1333 it.hapPath = std::regex_replace(it.hapPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
1334 }1334 }
@@ -2109,7 +2109,7 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con
2109 std::string loadPath =2109 std::string loadPath =
2110 entryHapModuleInfo.hapPath.empty() ? entryHapModuleInfo.resourcePath : entryHapModuleInfo.hapPath;2110 entryHapModuleInfo.hapPath.empty() ? entryHapModuleInfo.resourcePath : entryHapModuleInfo.hapPath;
2111 std::regex inner_pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + bundleInfo.name);2111 std::regex inner_pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + bundleInfo.name);
2112- loadPath = std::regex_replace(loadPath, inner_pattern, LOCAL_CODE_PATH);2112+ loadPath = ExtractorUtil::GetLoadFilePath(loadPath);
2113 application_->SetEntryLoadPath(loadPath);2113 application_->SetEntryLoadPath(loadPath);
2114 auto res = GetOverlayModuleInfos(bundleInfo.name, moduleName, overlayModuleInfos_);2114 auto res = GetOverlayModuleInfos(bundleInfo.name, moduleName, overlayModuleInfos_);
2115 std::vector<std::string> overlayPaths;2115 std::vector<std::string> overlayPaths;
@@ -2548,8 +2548,7 @@ void MainThread::ChangeToLocalPath(const std::string &bundleName,
2548 if (item.empty()) {2548 if (item.empty()) {
2549 continue;2549 continue;
2550 }2550 }
2551- localPath.emplace_back(2551+ localPath.emplace_back(ExtractorUtil::GetLoadFilePath(item));
2552- std::regex_replace(item, pattern, std::string(LOCAL_CODE_PATH) + std::string(FILE_SEPARATOR)));
2553 }2552 }
2554}2553}
2555 2554 
@@ -2568,7 +2567,7 @@ void MainThread::ChangeToLocalPath(const std::string &bundleName,
2568 localPath.c_str(), bundleName.c_str());2567 localPath.c_str(), bundleName.c_str());
2569 }2568 }
2570 if (isExist) {2569 if (isExist) {
2571- localPath = std::regex_replace(localPath, pattern, std::string(LOCAL_CODE_PATH));2570+ localPath = ExtractorUtil::GetLoadFilePath(localPath);
2572 } else {2571 } else {
2573 localPath = std::regex_replace(localPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);2572 localPath = std::regex_replace(localPath, std::regex(ABS_CODE_PATH), LOCAL_BUNDLES);
2574 }2573 }
@@ -134,9 +134,7 @@ std::string HybridJsModuleReader::GetPluginHspPath(const std::string& inputPath)
134 && sharedBundleName == pluginBundleInfo.pluginBundleName) {134 && sharedBundleName == pluginBundleInfo.pluginBundleName) {
135 presetAppHapPath = pluginModuleInfo.hapPath;135 presetAppHapPath = pluginModuleInfo.hapPath;
136 TAG_LOGD(AAFwkTag::JSRUNTIME, "presetAppHapPath %{public}s", presetAppHapPath.c_str());136 TAG_LOGD(AAFwkTag::JSRUNTIME, "presetAppHapPath %{public}s", presetAppHapPath.c_str());
137- std::regex pattern(std::string(ABS_DATA_CODE_PATH) + bundleName_ + "/");137+ presetAppHapPath = ExtractorUtil::GetLoadFilePath(presetAppHapPath);
138- presetAppHapPath = std::regex_replace(
139- presetAppHapPath, pattern, std::string(ABS_CODE_PATH) + std::string(BUNDLE));
140 TAG_LOGD(AAFwkTag::JSRUNTIME, "presetAppHapPath %{public}s", presetAppHapPath.c_str());138 TAG_LOGD(AAFwkTag::JSRUNTIME, "presetAppHapPath %{public}s", presetAppHapPath.c_str());
141 return presetAppHapPath;139 return presetAppHapPath;
142 }140 }
@@ -12,7 +12,7 @@
12 * See the License for the specific language governing permissions and12 * See the License for the specific language governing permissions and
13 * limitations under the License.13 * limitations under the License.
14 */14 */
15- 15+
16#include <regex>16#include <regex>
17#include "js_module_reader.h"17#include "js_module_reader.h"
18 18 
@@ -125,9 +125,7 @@ std::string JsModuleReader::GetPluginHspPath(const std::string& inputPath) const
125 && sharedBundleName == pluginBundleInfo.pluginBundleName) {125 && sharedBundleName == pluginBundleInfo.pluginBundleName) {
126 presetAppHapPath = pluginModuleInfo.hapPath;126 presetAppHapPath = pluginModuleInfo.hapPath;
127 TAG_LOGD(AAFwkTag::JSRUNTIME, "presetAppHapPath %{public}s", presetAppHapPath.c_str());127 TAG_LOGD(AAFwkTag::JSRUNTIME, "presetAppHapPath %{public}s", presetAppHapPath.c_str());
128- std::regex pattern(std::string(ABS_DATA_CODE_PATH) + bundleName_ + "/");128+ presetAppHapPath = ExtractorUtil::GetLoadFilePath(presetAppHapPath);
129- presetAppHapPath = std::regex_replace(
130- presetAppHapPath, pattern, std::string(ABS_CODE_PATH) + std::string(BUNDLE));
131 TAG_LOGD(AAFwkTag::JSRUNTIME, "presetAppHapPath %{public}s", presetAppHapPath.c_str());129 TAG_LOGD(AAFwkTag::JSRUNTIME, "presetAppHapPath %{public}s", presetAppHapPath.c_str());
132 return presetAppHapPath;130 return presetAppHapPath;
133 }131 }
@@ -286,10 +284,6 @@ void JsModuleReader::GetHapPathList(const std::string &bundleName, std::vector<s
286 return;284 return;
287 }285 }
288 auto bundleMgrProxy = iface_cast<IBundleMgr>(remoteObject);286 auto bundleMgrProxy = iface_cast<IBundleMgr>(remoteObject);
289- if (bundleMgrProxy == nullptr) {
290- TAG_LOGE(AAFwkTag::JSRUNTIME, "null bundleMgrProxy");
291- return;
292- }
293 AppExecFwk::BundleInfo bundleInfo;287 AppExecFwk::BundleInfo bundleInfo;
294 auto getInfoResult = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::288 auto getInfoResult = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::
A
Aafwk_helper7 天前

🤖 AI 代码检视意见(回复本评论可解决检视意见,点击被检视代码行左侧的小头像可收起检视意见)


🔴 删除 bundleMgrProxy 空指针检查导致潜在 NPE

位置: L288-L290 | 严重程度: Critical

❓ 问题描述

原代码在 iface_cast 之后立即检查 bundleMgrProxy 是否为空并 return;本次 PR 删除了该检查。若 remoteObject 无效或服务未就绪,bundleMgrProxy 将为 nullptr,随后调用 bundleMgrProxy->GetBundleInfoForSelf(...) 会直接解引用空指针导致进程崩溃。这正是 OpenHarmony 平台强调的 IPC 反序列化/代理空检查要求所禁止的。

💡 修复建议

修改建议:在 iface_cast 之后保留对 bundleMgrProxy 的空指针检查,避免对 nullptr 调用方法。

288: auto bundleMgrProxy = iface_cast(remoteObject); 289: if (bundleMgrProxy == nullptr) { 290: TAG_LOGE(AAFwkTag::JSRUNTIME, "null bundleMgrProxy"); 291: return; 292: } 293: AppExecFwk::BundleInfo bundleInfo;


likedislike
295 GET_BUNDLE_INFO_WITH_HAP_MODULE), bundleInfo);289 GET_BUNDLE_INFO_WITH_HAP_MODULE), bundleInfo);
@@ -302,4 +296,4 @@ void JsModuleReader::GetHapPathList(const std::string &bundleName, std::vector<s
302 }296 }
303}297}
304} // namespace AbilityRuntime298} // namespace AbilityRuntime
305-} // namespace OHOS299+} // namespace OHOS
@@ -2514,6 +2514,7 @@ private:
2514 const std::shared_ptr<AppRunningRecord> &appRecord, const UIExtensionProcessBindInfo &bindInfo);2514 const std::shared_ptr<AppRunningRecord> &appRecord, const UIExtensionProcessBindInfo &bindInfo);
2515 void UnBindUIExtensionProcess(2515 void UnBindUIExtensionProcess(
2516 const std::shared_ptr<AppRunningRecord> &appRecord, const UIExtensionProcessBindInfo &bindInfo);2516 const std::shared_ptr<AppRunningRecord> &appRecord, const UIExtensionProcessBindInfo &bindInfo);
2517+ std::string GetStoragePath(const std::string& hapPath);
2517 bool WrapBindInfo(std::shared_ptr<AAFwk::Want> &want, std::shared_ptr<AppRunningRecord> &appRecord,2518 bool WrapBindInfo(std::shared_ptr<AAFwk::Want> &want, std::shared_ptr<AppRunningRecord> &appRecord,
2518 UIExtensionProcessBindInfo &bindInfo);2519 UIExtensionProcessBindInfo &bindInfo);
2519 void ReportEventToRSS(const AppExecFwk::AbilityInfo &abilityInfo,2520 void ReportEventToRSS(const AppExecFwk::AbilityInfo &abilityInfo,
@@ -2262,8 +2262,8 @@ void AppMgrServiceInner::LoadAbility(std::shared_ptr<AbilityInfo> abilityInfo, s
2262 appInfo->bundleName = hostBundleName;2262 appInfo->bundleName = hostBundleName;
2263 std::regex pattern(2263 std::regex pattern(
2264 std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + hostBundleName);2264 std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + hostBundleName);
2265- abilityInfo->hapPath = std::regex_replace(abilityInfo->hapPath, pattern, LOCAL_CODE_PATH);2265+ abilityInfo->hapPath = GetStoragePath(abilityInfo->hapPath);
2266- abilityInfo->resourcePath = std::regex_replace(abilityInfo->resourcePath, pattern, LOCAL_CODE_PATH);2266+ abilityInfo->resourcePath = GetStoragePath(abilityInfo->resourcePath);
2267 GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo, appIndex);2267 GetBundleAndHapInfo(*abilityInfo, appInfo, bundleInfo, hapModuleInfo, appIndex);
2268 appInfo = std::make_shared<ApplicationInfo>(bundleInfo.applicationInfo);2268 appInfo = std::make_shared<ApplicationInfo>(bundleInfo.applicationInfo);
2269 auto pluginRet = DelayedSingleton<BundleMgrHelper>::GetInstance()->GetPluginHapModuleInfo(2269 auto pluginRet = DelayedSingleton<BundleMgrHelper>::GetInstance()->GetPluginHapModuleInfo(
@@ -13409,6 +13409,13 @@ void AppMgrServiceInner::CheckRenderAttachTimeout(std::shared_ptr<RenderRecord>
13409 ProcessStartFailedReason::ATTACH_TIMEOUT, elapsedMs);13409 ProcessStartFailedReason::ATTACH_TIMEOUT, elapsedMs);
13410}13410}
13411 13411 
13412+std::string AppMgrServiceInner::GetStoragePath(const std::string& hapPath) {
A
Aafwk_helper7 天前

🤖 AI 代码检视意见(回复本评论可解决检视意见,点击被检视代码行左侧的小头像可收起检视意见)


🟠 GetStoragePath 未校验 find 返回 npos 且语义与原 regex 转换不一致

位置: L13412-L13417 | 严重程度: High

❓ 问题描述

新函数 GetStoragePath 用 std::regex_replace 去掉 ABS_CODE_PATH+FILE_SEPARATOR,再调用 loadPath.find(FILE_SEPARATOR)+1 取子串。若 find 返回 std::string::npos,则 npos+1 在无符号运算下回绕为 0,substr(0) 返回整串,行为不直观且非预期;此外原调用点使用的是 ABS_CODE_PATH + FILE_SEPARATOR + hostBundleName 的精确 pattern,新实现完全忽略了 hostBundleName 维度,对非匹配 hostBundleName 的路径会产生错误结果。代码风格上还混用了 Tab 与空格缩进。

💡 修复建议

修改建议:显式判空与检查 npos,并保持与原 regex 转换的语义一致;统一使用 4 空格缩进,避免 Tab 混入。

13412: std::string AppMgrServiceInner::GetStoragePath(const std::string& hapPath) { 13413: if (hapPath.empty()) { 13414: return hapPath; 13415: } 13416: std::regex hapPattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR)); 13417: std::string loadPath = std::regex_replace(hapPath, hapPattern, ""); 13418: auto pos = loadPath.find(std::string(FILE_SEPARATOR)); 13419: if (pos == std::string::npos) { 13420: return std::string(LOCAL_CODE_PATH) + std::string(FILE_SEPARATOR) + loadPath; 13421: } 13422: loadPath = std::string(LOCAL_CODE_PATH) + std::string(FILE_SEPARATOR) + loadPath.substr(pos + 1); 13423: return loadPath; 13424: }


likedislike
13413+ std::regex hapPattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR));
13414+ std::string loadPath = std::regex_replace(hapPath, hapPattern, "");
13415+ loadPath = std::string(LOCAL_CODE_PATH) + std::string(FILE_SEPARATOR) + loadPath.substr(loadPath.find(std::string(FILE_SEPARATOR)) + 1);
13416+ return loadPath;
13417+}
13418+ 
13412void AppMgrServiceInner::HandleForegroundAbilityDied(13419void AppMgrServiceInner::HandleForegroundAbilityDied(
13413 const std::vector<sptr<IRemoteObject>>& abilityTokens,ApplicationState state)13420 const std::vector<sptr<IRemoteObject>>& abilityTokens,ApplicationState state)
13414{13421{