From 404c84a649314f4a96dd48598e1bcd44bb8d8e39 Mon Sep 17 00:00:00 2001 From: zhrenqiang Date: Fri, 12 Sep 2025 15:23:02 +0800 Subject: [PATCH] add innerBundleInfo try-catch Change-Id: I68d311418759b0f99f820c0474a9ecae9caa42b9 Signed-off-by: zhrenqiang --- .../bundlemgr/src/bundle_data_storage_rdb.cpp | 14 +- services/bundlemgr/src/inner_bundle_info.cpp | 541 +++++++++--------- .../bms_bundle_data_storage_database_test.cpp | 15 + .../bms_rdb_data_manager_test.cpp | 25 + 4 files changed, 328 insertions(+), 267 deletions(-) diff --git a/services/bundlemgr/src/bundle_data_storage_rdb.cpp b/services/bundlemgr/src/bundle_data_storage_rdb.cpp index ada0fe64e4..28403de370 100644 --- a/services/bundlemgr/src/bundle_data_storage_rdb.cpp +++ b/services/bundlemgr/src/bundle_data_storage_rdb.cpp @@ -132,8 +132,13 @@ bool BundleDataStorageRdb::SaveStorageBundleInfo(const InnerBundleInfo &innerBun } LOG_NOFUNC_I(BMS_TAG_COMMON, "rdb SaveStorageBundleInfo -n %{public}s", innerBundleInfo.GetBundleName().c_str()); + std::string value = innerBundleInfo.ToString(); + if (value.empty()) { + LOG_NOFUNC_E(BMS_TAG_COMMON, "err: empty str"); + return false; + } bool ret = rdbDataManager_->InsertData( - innerBundleInfo.GetBundleName(), innerBundleInfo.ToString()); + innerBundleInfo.GetBundleName(), value); return ret; } @@ -146,8 +151,13 @@ ErrCode BundleDataStorageRdb::SaveStorageBundleInfoWithCode(const InnerBundleInf } LOG_NOFUNC_I(BMS_TAG_COMMON, "rdb SaveStorageBundleInfo -n %{public}s", innerBundleInfo.GetBundleName().c_str()); + std::string value = innerBundleInfo.ToString(); + if (value.empty()) { + LOG_NOFUNC_E(BMS_TAG_COMMON, "err: empty str"); + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } return rdbDataManager_->InsertDataWithCode( - innerBundleInfo.GetBundleName(), innerBundleInfo.ToString()); + innerBundleInfo.GetBundleName(), value); } diff --git a/services/bundlemgr/src/inner_bundle_info.cpp b/services/bundlemgr/src/inner_bundle_info.cpp index 9f5713b3ff..41ff08083d 100644 --- a/services/bundlemgr/src/inner_bundle_info.cpp +++ b/services/bundlemgr/src/inner_bundle_info.cpp @@ -23,6 +23,7 @@ #endif #include "app_log_tag_wrapper.h" #include "bms_extension_data_mgr.h" +#include "bundle_constants.h" #include "bundle_extractor.h" #include "bundle_mgr_client.h" #include "bundle_permission_mgr.h" @@ -1314,269 +1315,274 @@ void from_json(const nlohmann::json &jsonObject, Dependency &dependency) int32_t InnerBundleInfo::FromJson(const nlohmann::json &jsonObject) { - const auto &jsonObjectEnd = jsonObject.end(); - int32_t parseResult = ERR_OK; - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - APP_TYPE, - appType_, - JsonType::NUMBER, - true, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - BUNDLE_STATUS, - bundleStatus_, - JsonType::NUMBER, - false, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - ALLOWED_ACLS, - allowedAcls_, - JsonType::ARRAY, - false, - parseResult, - ArrayType::STRING); - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - BASE_BUNDLE_INFO, - *baseBundleInfo_, - JsonType::OBJECT, - true, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - BASE_APPLICATION_INFO, - *baseApplicationInfo_, - JsonType::OBJECT, - true, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - BASE_ABILITY_INFO, - baseAbilityInfos_, - JsonType::OBJECT, - true, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - INNER_MODULE_INFO, - innerModuleInfos_, - JsonType::OBJECT, - true, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>>(jsonObject, - jsonObjectEnd, - INNER_SHARED_MODULE_INFO, - innerSharedModuleInfos_, - JsonType::OBJECT, - false, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>>(jsonObject, - jsonObjectEnd, - SKILL_INFOS, - skillInfos_, - JsonType::OBJECT, - true, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - USER_ID, - userId_, - JsonType::NUMBER, - true, - parseResult, - ArrayType::NOT_ARRAY); - BMSJsonUtil::GetStrValueIfFindKey(jsonObject, - jsonObjectEnd, - APP_FEATURE, - appFeature_, - true, - parseResult); - GetValueIfFindKey>>(jsonObject, - jsonObjectEnd, - MODULE_FORMS, - formInfos_, - JsonType::OBJECT, - true, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - MODULE_SHORTCUT, - shortcutInfos_, - JsonType::OBJECT, - true, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - MODULE_COMMON_EVENT, - commonEvents_, - JsonType::OBJECT, - true, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - INSTALL_MARK, - mark_, - JsonType::OBJECT, - false, - parseResult, - ArrayType::NOT_ARRAY); - int32_t isOldVersion = ERR_OK; - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - INNER_BUNDLE_USER_INFOS, - innerBundleUserInfos_, - JsonType::OBJECT, - true, - isOldVersion, - ArrayType::NOT_ARRAY); - if (parseResult == ERR_OK && isOldVersion == ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP) { - // To be compatible with the old database, - // if the old data does not have bundleUserInfos, - // the default user information needs to be constructed. - BuildDefaultUserInfo(); - } - BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, - jsonObjectEnd, - BUNDLE_IS_NEW_VERSION, - isNewVersion_, - false, - parseResult); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - BUNDLE_BASE_EXTENSION_INFOS, - baseExtensionInfos_, - JsonType::OBJECT, - false, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>>(jsonObject, - jsonObjectEnd, - BUNDLE_EXTENSION_SKILL_INFOS, - extensionSkillInfos_, - JsonType::OBJECT, - false, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - BUNDLE_EXTEND_RESOURCES, - extendResourceInfos_, - JsonType::OBJECT, - false, - parseResult, - ArrayType::NOT_ARRAY); - BMSJsonUtil::GetStrValueIfFindKey(jsonObject, - jsonObjectEnd, - CUR_DYNAMIC_ICON_MODULE, - curDynamicIconModule_, - false, - parseResult); - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - BUNDLE_PACK_INFO, - *bundlePackInfo_, - JsonType::OBJECT, - false, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - APP_INDEX, - appIndex_, - JsonType::NUMBER, - false, - parseResult, - ArrayType::NOT_ARRAY); - BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, - jsonObjectEnd, - BUNDLE_IS_SANDBOX_APP, - isSandboxApp_, - false, - parseResult); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - BUNDLE_HQF_INFOS, - hqfInfos_, - JsonType::ARRAY, - false, - parseResult, - ArrayType::OBJECT); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - OVERLAY_BUNDLE_INFO, - overlayBundleInfo_, - JsonType::ARRAY, - false, - parseResult, - ArrayType::OBJECT); - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - OVERLAY_TYPE, - overlayType_, - JsonType::NUMBER, - false, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey(jsonObject, - jsonObjectEnd, - APPLY_QUICK_FIX_FREQUENCY, - applyQuickFixFrequency_, - JsonType::NUMBER, - false, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>>(jsonObject, - jsonObjectEnd, - DATA_GROUP_INFOS, - dataGroupInfos_, - JsonType::OBJECT, - false, - parseResult, - ArrayType::NOT_ARRAY); - GetValueIfFindKey>(jsonObject, - jsonObjectEnd, - PLUGIN_BUNDLE_INFOS, - pluginBundleInfos_, - JsonType::OBJECT, - false, - parseResult, - ArrayType::NOT_ARRAY); - BMSJsonUtil::GetStrValueIfFindKey(jsonObject, - jsonObjectEnd, - DEVELOPER_ID, - developerId_, - false, - parseResult); - BMSJsonUtil::GetStrValueIfFindKey(jsonObject, - jsonObjectEnd, - ODID, - odid_, - false, - parseResult); - BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, - jsonObjectEnd, - UNINSTALL_STATE, - uninstallState_, - false, - parseResult); - if (parseResult != ERR_OK) { - APP_LOGE("read InnerBundleInfo from database error code : %{public}d", parseResult); + try { + const auto &jsonObjectEnd = jsonObject.end(); + int32_t parseResult = ERR_OK; + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + APP_TYPE, + appType_, + JsonType::NUMBER, + true, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + BUNDLE_STATUS, + bundleStatus_, + JsonType::NUMBER, + false, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + ALLOWED_ACLS, + allowedAcls_, + JsonType::ARRAY, + false, + parseResult, + ArrayType::STRING); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + BASE_BUNDLE_INFO, + *baseBundleInfo_, + JsonType::OBJECT, + true, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + BASE_APPLICATION_INFO, + *baseApplicationInfo_, + JsonType::OBJECT, + true, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + BASE_ABILITY_INFO, + baseAbilityInfos_, + JsonType::OBJECT, + true, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + INNER_MODULE_INFO, + innerModuleInfos_, + JsonType::OBJECT, + true, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>>(jsonObject, + jsonObjectEnd, + INNER_SHARED_MODULE_INFO, + innerSharedModuleInfos_, + JsonType::OBJECT, + false, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>>(jsonObject, + jsonObjectEnd, + SKILL_INFOS, + skillInfos_, + JsonType::OBJECT, + true, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + USER_ID, + userId_, + JsonType::NUMBER, + true, + parseResult, + ArrayType::NOT_ARRAY); + BMSJsonUtil::GetStrValueIfFindKey(jsonObject, + jsonObjectEnd, + APP_FEATURE, + appFeature_, + true, + parseResult); + GetValueIfFindKey>>(jsonObject, + jsonObjectEnd, + MODULE_FORMS, + formInfos_, + JsonType::OBJECT, + true, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + MODULE_SHORTCUT, + shortcutInfos_, + JsonType::OBJECT, + true, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + MODULE_COMMON_EVENT, + commonEvents_, + JsonType::OBJECT, + true, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSTALL_MARK, + mark_, + JsonType::OBJECT, + false, + parseResult, + ArrayType::NOT_ARRAY); + int32_t isOldVersion = ERR_OK; + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + INNER_BUNDLE_USER_INFOS, + innerBundleUserInfos_, + JsonType::OBJECT, + true, + isOldVersion, + ArrayType::NOT_ARRAY); + if (parseResult == ERR_OK && isOldVersion == ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP) { + // To be compatible with the old database, + // if the old data does not have bundleUserInfos, + // the default user information needs to be constructed. + BuildDefaultUserInfo(); + } + BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, + jsonObjectEnd, + BUNDLE_IS_NEW_VERSION, + isNewVersion_, + false, + parseResult); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + BUNDLE_BASE_EXTENSION_INFOS, + baseExtensionInfos_, + JsonType::OBJECT, + false, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>>(jsonObject, + jsonObjectEnd, + BUNDLE_EXTENSION_SKILL_INFOS, + extensionSkillInfos_, + JsonType::OBJECT, + false, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + BUNDLE_EXTEND_RESOURCES, + extendResourceInfos_, + JsonType::OBJECT, + false, + parseResult, + ArrayType::NOT_ARRAY); + BMSJsonUtil::GetStrValueIfFindKey(jsonObject, + jsonObjectEnd, + CUR_DYNAMIC_ICON_MODULE, + curDynamicIconModule_, + false, + parseResult); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + BUNDLE_PACK_INFO, + *bundlePackInfo_, + JsonType::OBJECT, + false, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + APP_INDEX, + appIndex_, + JsonType::NUMBER, + false, + parseResult, + ArrayType::NOT_ARRAY); + BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, + jsonObjectEnd, + BUNDLE_IS_SANDBOX_APP, + isSandboxApp_, + false, + parseResult); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + BUNDLE_HQF_INFOS, + hqfInfos_, + JsonType::ARRAY, + false, + parseResult, + ArrayType::OBJECT); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + OVERLAY_BUNDLE_INFO, + overlayBundleInfo_, + JsonType::ARRAY, + false, + parseResult, + ArrayType::OBJECT); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + OVERLAY_TYPE, + overlayType_, + JsonType::NUMBER, + false, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + APPLY_QUICK_FIX_FREQUENCY, + applyQuickFixFrequency_, + JsonType::NUMBER, + false, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>>(jsonObject, + jsonObjectEnd, + DATA_GROUP_INFOS, + dataGroupInfos_, + JsonType::OBJECT, + false, + parseResult, + ArrayType::NOT_ARRAY); + GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + PLUGIN_BUNDLE_INFOS, + pluginBundleInfos_, + JsonType::OBJECT, + false, + parseResult, + ArrayType::NOT_ARRAY); + BMSJsonUtil::GetStrValueIfFindKey(jsonObject, + jsonObjectEnd, + DEVELOPER_ID, + developerId_, + false, + parseResult); + BMSJsonUtil::GetStrValueIfFindKey(jsonObject, + jsonObjectEnd, + ODID, + odid_, + false, + parseResult); + BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, + jsonObjectEnd, + UNINSTALL_STATE, + uninstallState_, + false, + parseResult); + if (parseResult != ERR_OK) { + APP_LOGE("read InnerBundleInfo from database error code : %{public}d", parseResult); + } + return parseResult; + } catch (const nlohmann::json::exception &e) { + APP_LOGE("FromJson err: %{public}s", e.what()); + return ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; } - return parseResult; } void InnerBundleInfo::BuildDefaultUserInfo() @@ -2426,9 +2432,14 @@ void InnerBundleInfo::RemoveModuleInfo(const std::string &modulePackage) std::string InnerBundleInfo::ToString() const { - nlohmann::json j; - ToJson(j); - return j.dump(); + try { + nlohmann::json j; + ToJson(j); + return j.dump(); + } catch (const nlohmann::json::exception &e) { + LOG_E(BMS_TAG_DEFAULT, "ToString err: %{public}s", e.what()); + return Constants::EMPTY_STRING; + } } void InnerBundleInfo::GetApplicationInfo(int32_t flags, int32_t userId, ApplicationInfo &appInfo, diff --git a/services/bundlemgr/test/unittest/bms_bundle_data_storage_test/bms_bundle_data_storage_database_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_data_storage_test/bms_bundle_data_storage_database_test.cpp index 87deeca74e..4ac402bcea 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_data_storage_test/bms_bundle_data_storage_database_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_data_storage_test/bms_bundle_data_storage_database_test.cpp @@ -6214,4 +6214,19 @@ HWTEST_F(BmsBundleDataStorageDatabaseTest, ConvertToAbilityInfo_0001, Function | AbilityInfo abilityInfo = InnerAbilityInfo::ConvertToAbilityInfo(innerAbilityInfo); CheckAbilityInfo(abilityInfo); } + +/** + * @tc.number: InnerBundleInfoToString_0001 + * @tc.name: test InnerBundleInfoToString_0001 + * @tc.desc: 1. test InnerBundleInfo::ToString() + */ +HWTEST_F(BmsBundleDataStorageDatabaseTest, InnerBundleInfoToString_0001, Function | SmallTest | Level1) +{ + InnerBundleInfo innerBundleInfo; + std::string data = innerBundleInfo.ToString(); + EXPECT_FALSE(data.empty()); + innerBundleInfo.baseBundleInfo_->description = "\xC4\xE3\xBA\xCA"; + std::string dataEmpty = innerBundleInfo.ToString(); + EXPECT_TRUE(dataEmpty.empty()); +} } // OHOS \ No newline at end of file diff --git a/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp b/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp index 08354d2d5f..89dbcb46eb 100755 --- a/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp +++ b/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp @@ -262,6 +262,31 @@ HWTEST_F(BmsRdbDataManagerTest, BundleDataStorageRdb_0400, Function | SmallTest EXPECT_TRUE(ret); } +/** + * @tc.number: SaveStorageBundleInfo_0100 + * @tc.name: SaveStorageBundleInfo_0100 + * @tc.desc: 1.SaveStorageBundleInfo_0100 + * @tc.require: SaveStorageBundleInfo_0100 + */ +HWTEST_F(BmsRdbDataManagerTest, SaveStorageBundleInfo_0100, Function | SmallTest | Level1) +{ + std::shared_ptr dataStorage = std::make_shared(); + ASSERT_NE(dataStorage, nullptr); + InnerBundleInfo innerBundleInfo; + bool ret = dataStorage->SaveStorageBundleInfo(innerBundleInfo); + EXPECT_TRUE(ret); + + ErrCode errCode = dataStorage->SaveStorageBundleInfoWithCode(innerBundleInfo); + EXPECT_EQ(errCode, ERR_OK); + + innerBundleInfo.baseBundleInfo_->description = "\xC4\xE3\xBA\xCA"; + ret = dataStorage->SaveStorageBundleInfo(innerBundleInfo); + EXPECT_FALSE(ret); + + errCode = dataStorage->SaveStorageBundleInfoWithCode(innerBundleInfo); + EXPECT_EQ(errCode, ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR); +} + /** * @tc.number: BundleDataStorageRdb_0500 * @tc.name: DeleteStorageBundleInfo -- Gitee