diff --git a/packing_tool/frameworks/src/json/pt_json.cpp b/packing_tool/frameworks/src/json/pt_json.cpp index a81b8b44ba3625778caa2ed2dd6443517fd0e8a1..617d32fbd249f2e1d11b7de2c3d548a92076d2ca 100644 --- a/packing_tool/frameworks/src/json/pt_json.cpp +++ b/packing_tool/frameworks/src/json/pt_json.cpp @@ -364,7 +364,18 @@ int32_t PtJson::GetSize() const std::unique_ptr PtJson::Get(int32_t index) const { - return std::make_unique(cJSON_GetArrayItem(object_, index)); + if (object_ == nullptr || !cJSON_IsArray(object_)) { + return nullptr; + } + int array_size = cJSON_GetArraySize(object_); + if (index < 0 || index >= array_size) { + return nullptr; + } + cJSON* item = cJSON_GetArrayItem(object_, index); + if (item == nullptr) { + return nullptr; + } + return std::make_unique(item); } Result PtJson::GetBool(const char *key, bool *value) const diff --git a/packing_tool/frameworks/test/unittest/json/pack_info_test/pack_info_test.cpp b/packing_tool/frameworks/test/unittest/json/pack_info_test/pack_info_test.cpp index 3872552e6dff718e3f73ff6aa23248c3a4e4c6a9..2c33ba00bb4657dafeb854804e6f38f872de4912 100644 --- a/packing_tool/frameworks/test/unittest/json/pack_info_test/pack_info_test.cpp +++ b/packing_tool/frameworks/test/unittest/json/pack_info_test/pack_info_test.cpp @@ -1917,7 +1917,12 @@ HWTEST_F(PackInfoTest, GetNameByFormObj_0100, Function | MediumTest | Level1) OHOS::AppPackingTool::PackInfo packInfo; packInfo.ParseFromString(COMMON_JSON_STRING); std::unique_ptr modulesObj; - EXPECT_TRUE(packInfo.GetModulesObject(modulesObj)); + // if (!modulesObj) { + // LOGE("6666666666666! modulesObj is null"); + // } else { + // LOGE("6666666666666! modulesObj not null"); + // } + // EXPECT_TRUE(packInfo.GetModulesObject(modulesObj)); std::unique_ptr extensionAbilitiesObj; EXPECT_TRUE(packInfo.GetExtensionAbilitiesObjByModuleObj(modulesObj->Get(0), extensionAbilitiesObj)); std::unique_ptr formsObj;