diff --git a/BUILD.gn b/BUILD.gn index df2cceb6fd47fe3685e2abe6acb97a87ac200f35..8847b1838e398744141e58a8a28625187b38b1d4 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -29,6 +29,7 @@ sources_platform_common = [ "./src/create_pcid.c", "./src/endian_internal.c", "./src/context_tool.c", + "./src/common_method.c", ] ohos_executable("syscap_tool_bin") { @@ -97,6 +98,7 @@ if (defined(ohos_lite)) { "./src/context_tool.c", "./src/endian_internal.c", "./src/syscap_tool.c", + "./src/common_method.c", ] deps += [ @@ -122,6 +124,7 @@ if (defined(ohos_lite)) { "./src/context_tool.c", "./src/endian_internal.c", "./src/syscap_tool.c", + "./src/common_method.c", ] external_deps = [ "bounds_checking_function:libsec_static", @@ -163,7 +166,7 @@ ohos_prebuilt_etc("pcid.sc") { } if (syscap_codec_config_extern_path != "") { - # generate syscap_define_custom.h +# generate syscap_define_custom.h action("gen_syscap_define_custom") { script = "./tools/syscap_config_merge.py" args = [ diff --git a/include/codec_config/syscap_define.h b/include/codec_config/syscap_define.h index cbb31669f0a4c021af1a1afe649004dede468ae8..3f9da061c4fecf71ea976f0f0fbada887437e0f1 100644 --- a/include/codec_config/syscap_define.h +++ b/include/codec_config/syscap_define.h @@ -352,8 +352,9 @@ typedef enum SystemCapabilityNum { COMMUNICATION_NETMANAGER_NETFIREWALL, CUSTOMIZATION_CUSTOMCONFIG, ABILITY_APPEXTENSION_PHOTOEDITOREXTENSION, - HIVIEWDFX_HICOLLIE, BUNDLEMANAGER_APPDOMAINVERIFY, + HIVIEWDFX_HICOLLIE, + NOTIFICATION_NOTIFICATIONSETTINGS, SECURITY_CRYPTOFRAMEWORK_KEY, SECURITY_CRYPTOFRAMEWORK_KEY_SYMKEY, SECURITY_CRYPTOFRAMEWORK_KEY_ASYMKEY, @@ -601,7 +602,8 @@ const static SyscapWithNum g_arraySyscap[] = { {"SystemCapability.Security.Cipher", SECURITY_CIPHER}, {"SystemCapability.Applications.Settings.Core", APPLICATIONS_SETTINGS_CORE}, {"SystemCapability.FileManagement.UserFileManager.Core", FILEMANAGEMENT_USERFILEMANAGER_CORE}, - {"SystemCapability.FileManagement.UserFileManager.DistributedCore", FILEMANAGEMENT_USERFILEMANAGER_DISTRIBUTEDCORE}, + {"SystemCapability.FileManagement.UserFileManager.DistributedCore", + FILEMANAGEMENT_USERFILEMANAGER_DISTRIBUTEDCORE}, {"SystemCapability.ResourceSchedule.BackgroundTaskManager.EfficiencyResourcesApply", RESOURCESCHEDULE_BACKGROUNDTASKMANAGER_EFFICIENCYRESOURCESAPPLY}, {"SystemCapability.Security.SecurityGuard", SECURITY_SECURITYGUARD}, @@ -679,7 +681,8 @@ const static SyscapWithNum g_arraySyscap[] = { {"SystemCapability.Print.PrintFramework.NativePrint", PRINT_PRINT_FRAMEWORK_NATIVEPRINT}, {"SystemCapability.ArkUi.Graphics3D", ARKUI_GRAPHICS3D}, {"SystemCapability.FileManagement.File.Environment.FolderObtain", FILEMANAGEMENT_FILE_ENVIRONMENT_FOLDEROBTAIN}, - {"SystemCapability.FileManagement.UserFileService.FolderSelection", FILEMANAGEMENT_USERFILESERVICE_FOLDERSELECTION}, + {"SystemCapability.FileManagement.UserFileService.FolderSelection", + FILEMANAGEMENT_USERFILESERVICE_FOLDERSELECTION}, {"SystemCapability.FileManagement.AppFileService.FolderAuthorization", FILEMANAGEMENT_APPFILESERVICE_FOLDERAUTHORIZATION}, {"SystemCapability.Graphics.Drawing", GRAPHICS_DRAWING}, @@ -702,6 +705,7 @@ const static SyscapWithNum g_arraySyscap[] = { {"SystemCapability.Customization.CustomConfig", CUSTOMIZATION_CUSTOMCONFIG}, {"SystemCapability.Ability.AppExtension.PhotoEditorExtension", ABILITY_APPEXTENSION_PHOTOEDITOREXTENSION}, {"SystemCapability.BundleManager.AppDomainVerify", BUNDLEMANAGER_APPDOMAINVERIFY}, + {"SystemCapability.Notification.NotificationSettings", NOTIFICATION_NOTIFICATIONSETTINGS}, {"SystemCapability.Security.CryptoFramework.Key", SECURITY_CRYPTOFRAMEWORK_KEY}, {"SystemCapability.Security.CryptoFramework.Key.SymKey", SECURITY_CRYPTOFRAMEWORK_KEY_SYMKEY}, {"SystemCapability.Security.CryptoFramework.Key.AsymKey", SECURITY_CRYPTOFRAMEWORK_KEY_ASYMKEY}, diff --git a/include/context_tool.h b/include/context_tool.h index b8b437a10dd008ae47bf33696c52a6ba54eedc10..2160be70ea3e5d1dda9aec119df9c51e5dcc7eb5 100644 --- a/include/context_tool.h +++ b/include/context_tool.h @@ -40,6 +40,7 @@ typedef struct RequiredProductCompatibilityIDHead { void FreeContextBuffer(char *contextBuffer); int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen); +int32_t CheckFileAndGetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen); int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, char *convertedBuffer, size_t contextBufLen); int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len); cJSON *CreateWholeSyscapJsonObj(void); diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index 017eb5b7523edf5f04b7d9d111ae090cc219db0f..9cc249948fc5f86e37b6764e60e3c91249b5b0c1 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -26,6 +26,7 @@ #include "endian_internal.h" #include "syscap_interface.h" #include "context_tool.h" +#include "common_method.h" #ifdef SYSCAP_DEFINE_EXTERN_ENABLE #include "syscap_define_custom.h" @@ -292,6 +293,10 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t); RPCIDHead *rpcidHeader = (RPCIDHead *)input; cJSON *sysCapJson = cJSON_CreateArray(); + if (sysCapJson == NULL) { + PRINT_ERR("Get sysCapJson failed, sysCapJson is empty.\n"); + ret = -1; + } for (i = 0; i < sysCapCount; i++) { char *temp = sysCapBegin + i * SINGLE_FEAT_LEN; if (strlen(temp) >= SINGLE_FEAT_LEN) { @@ -330,7 +335,7 @@ FREE_SYSCAP_OUT: } static int32_t TransStringFormatAndSaveSyscap(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, - cJSON *sysCapArray, const char *inputFile) + cJSON *sysCapArray, const char *inputFile) { // trans to string format sysCapArray = cJSON_GetObjectItem(freeAfterDecodeRpcidInfo.rpcidRoot, "syscap"); @@ -345,7 +350,7 @@ static int32_t TransStringFormatAndSaveSyscap(struct FreeAfterDecodeRpcidInfo fr } // malloc for save os syscap index freeAfterDecodeRpcidInfo.osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) - * freeAfterDecodeRpcidInfo.sysCapArraySize); + * freeAfterDecodeRpcidInfo.sysCapArraySize); if (freeAfterDecodeRpcidInfo.osSysCapIndex == NULL) { PRINT_ERR("malloc failed.\n"); return -1; @@ -355,7 +360,7 @@ static int32_t TransStringFormatAndSaveSyscap(struct FreeAfterDecodeRpcidInfo fr } static void PrintResultToOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer, - char *priSyscapArray, uint16_t indexOs, uint16_t indexPri) + char *priSyscapArray, uint16_t indexOs, uint16_t indexPri) { int32_t ret = 0; uint32_t i; @@ -402,7 +407,7 @@ static void PrintResultToOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDeco } static void PartSysCapAndOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer, - char *priSyscapArray, cJSON *sysCapArray) + char *priSyscapArray, cJSON *sysCapArray) { uint32_t i; int32_t ret = 0; @@ -439,11 +444,11 @@ static void PartSysCapAndOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDeco } static char *FreeAfterDecodeRpcidToString(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, int32_t type, - char *outBuffer) + char *outBuffer) { switch (type) { case FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID: - free(freeAfterDecodeRpcidInfo.priSyscap); + SafeFree(freeAfterDecodeRpcidInfo.priSyscap); free(freeAfterDecodeRpcidInfo.osSysCapIndex); cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine); cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot); @@ -495,6 +500,11 @@ char *DecodeRpcidToStringFormat(const char *inputFile) // parse rpcid to json freeAfterDecodeRpcidInfo.rpcidRoot = cJSON_CreateObject(); + if (freeAfterDecodeRpcidInfo.rpcidRoot == NULL) { + PRINT_ERR("Failed to create cJSON object for rpcidRoot\n"); + return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_CONTEXT_OUT_AFTER_DECODE_RPCID, outBuffer); + } + if (ParseRpcidToJson(freeAfterDecodeRpcidInfo.contextBuffer, bufferLen, freeAfterDecodeRpcidInfo.rpcidRoot)) { PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile); return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_RPCID_ROOT_AFTER_DECODE_RPCID, outBuffer); @@ -511,7 +521,7 @@ char *DecodeRpcidToStringFormat(const char *inputFile) if (priSyscapArray == NULL) { PRINT_ERR("malloc(%u) failed.\n", (uint32_t)freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN); return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_MALLOC_OSSYSCAP_AFTER_DECODE_RPCID, - outBuffer); + outBuffer); } PartSysCapAndOutBuffer(freeAfterDecodeRpcidInfo, outBuffer, priSyscapArray, sysCapArray); @@ -519,7 +529,7 @@ char *DecodeRpcidToStringFormat(const char *inputFile) } static int32_t CopySyscopToRet(struct PcidPriSyscapInfo *pcidPriSyscapInfo, const size_t allSyscapNum, - char *tempSyscap, uint32_t i, uint8_t k) + char *tempSyscap, uint32_t i, uint8_t k) { uint32_t pos = (i - 2) * INT_BIT + k; uint32_t t; @@ -540,7 +550,7 @@ static int32_t CopySyscopToRet(struct PcidPriSyscapInfo *pcidPriSyscapInfo, cons } static int32_t CheckPcidEachBit(struct PcidPriSyscapInfo *pcidPriSyscapInfo, CompareError *result, - const size_t allSyscapNum, uint32_t i, uint32_t blockBits) + const size_t allSyscapNum, uint32_t i, uint32_t blockBits) { int32_t ret = 0; for (uint8_t k = 0; k < INT_BIT; k++) { @@ -565,8 +575,8 @@ static int32_t CheckPcidEachBit(struct PcidPriSyscapInfo *pcidPriSyscapInfo, Com } static int32_t ComparePcidWithOsSyscap(struct PcidPriSyscapInfo *pcidPriSyscapInfo, - const uint32_t *pcidOsAarry, const uint32_t *rpcidOsAarry, CompareError *result, - const size_t allSyscapNum) + const uint32_t *pcidOsAarry, const uint32_t *rpcidOsAarry, CompareError *result, + const size_t allSyscapNum) { uint32_t i; int32_t ret = 0; @@ -585,7 +595,7 @@ static int32_t ComparePcidWithOsSyscap(struct PcidPriSyscapInfo *pcidPriSyscapIn } static int32_t ComparePcidWithPriSyscap(struct PcidPriSyscapInfo pcidPriSyscapInfo, CompareError *result, - uint16_t versionFlag) + uint16_t versionFlag) { uint32_t i, j; uint16_t prisyscapFlag = 0; @@ -608,13 +618,19 @@ static int32_t ComparePcidWithPriSyscap(struct PcidPriSyscapInfo pcidPriSyscapIn return -1; } pcidPriSyscapInfo.ret = strcpy_s(temp, sizeof(char) * SINGLE_SYSCAP_LEN, - pcidPriSyscapInfo.rpcidPriSyscap + SINGLE_SYSCAP_LEN * i); + pcidPriSyscapInfo.rpcidPriSyscap + SINGLE_SYSCAP_LEN * i); if (pcidPriSyscapInfo.ret != EOK) { FreeCompareError(result); PRINT_ERR("strcpy_s failed.\n"); free(temp); return -1; } + if (pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag >= MAX_MISS_SYSCAP) { + FreeCompareError(result); + PRINT_ERR("array index out of bounds.\n"); + free(temp); + return -1; + } result->syscap[pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag] = temp; ++prisyscapFlag; } @@ -647,9 +663,9 @@ int32_t ComparePcidString(const char *pcidString, const char *rpcidString, Compa const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); pcidPriSyscapInfo.ret = SeparateSyscapFromString(pcidString, pcidOsAarry, PCID_OUT_BUFFER, - &pcidPriSyscapInfo.pcidPriSyscap, &pcidPriSyscapInfo.pcidPriSyscapLen); + &pcidPriSyscapInfo.pcidPriSyscap, &pcidPriSyscapInfo.pcidPriSyscapLen); pcidPriSyscapInfo.ret += SeparateSyscapFromString(rpcidString, rpcidOsAarry, RPCID_OUT_BUFFER, - &pcidPriSyscapInfo.rpcidPriSyscap, &pcidPriSyscapInfo.rpcidPriSyscapLen); + &pcidPriSyscapInfo.rpcidPriSyscap, &pcidPriSyscapInfo.rpcidPriSyscapLen); if (pcidPriSyscapInfo.ret != 0) { PRINT_ERR("Separate syscap from string failed. ret = %d\n", pcidPriSyscapInfo.ret); return -1; diff --git a/interfaces/inner_api/syscap_interface.h b/interfaces/inner_api/syscap_interface.h index a26bfae415257e5679e97e31f68875786ab419de..f0c4bd59494af4f914ede45ede26b7271bea294b 100644 --- a/interfaces/inner_api/syscap_interface.h +++ b/interfaces/inner_api/syscap_interface.h @@ -26,7 +26,7 @@ #define PCID_MAIN_BYTES 128 #define PCID_MAIN_INTS 32 -#define E_EORROR (-1) +#define E_ERROR (-1) #define E_OK 0 #define E_APIVERSION 1 #define E_SYSCAP 2 @@ -54,7 +54,7 @@ char *DecodeRpcidToStringFormat(const char *inputFile); * rpcidString, input string format rpcid. * result, output comparison results. * retval: - * E_EORROR, compare failed. + * E_ERROR, compare failed. * E_OK, compare successful and meet the requirements. * E_APIVERSION, compare successful but api version too low. * E_SYSCAP, compare successful but missing some syscaps. diff --git a/src/common_method.c b/src/common_method.c new file mode 100755 index 0000000000000000000000000000000000000000..cf7880f668cf97524f2adc221be591510b2a197d --- /dev/null +++ b/src/common_method.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common_method.h" +#include + +void SafeFree(char *pointer) +{ + if ((pointer) != NULL) { + free(pointer); + } +} diff --git a/src/common_method.h b/src/common_method.h new file mode 100755 index 0000000000000000000000000000000000000000..a9030bbc6af74059c1cc4e928e7537542bb7f3b2 --- /dev/null +++ b/src/common_method.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef COMMON_METHOD_H +#define COMMON_METHOD_H + +#include + +void SafeFree(char *pointer); + +#endif \ No newline at end of file diff --git a/src/context_tool.c b/src/context_tool.c index 1440a6bc3c60a28a667742b63f39c41140d630af..bb243efca2946bd65c89b7b6fe5e70eaaf647c86 100644 --- a/src/context_tool.c +++ b/src/context_tool.c @@ -92,6 +92,19 @@ int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bu return 0; } +int32_t CheckFileAndGetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen) +{ + if (inputFile == NULL) { + PRINT_ERR("input file is NULL.\n"); + return -1; + } + int32_t ret = GetFileContext(inputFile, contextBufPtr, bufferLen); + if (ret != 0) { + PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); + } + return ret; +} + int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, char *convertedBuffer, size_t contextBufLen) { int32_t ret; @@ -155,23 +168,25 @@ int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len) } if (bufferLen < (2 * sizeof(uint32_t))) { // 2, header of rpcid.sc PRINT_ERR("Parse file failed(format is invalid), input file : %s\n", inputFile); - free(contextBuffer); + FreeContextBuffer(contextBuffer); return -1; } rpcidHeader = (RPCIDHead *)contextBuffer; if (rpcidHeader->apiVersionType != 1) { PRINT_ERR("Parse file failed(apiVersionType != 1), input file : %s\n", inputFile); - free(contextBuffer); + FreeContextBuffer(contextBuffer); return -1; } sysCaptype = NtohsInter(*(uint16_t *)(rpcidHeader + 1)); if (sysCaptype != 2) { // 2, app syscap type PRINT_ERR("Parse file failed(sysCaptype != 2), input file : %s\n", inputFile); + FreeContextBuffer(contextBuffer); return -1; } sysCapLength = NtohsInter(*(uint16_t *)((char *)(rpcidHeader + 1) + sizeof(uint16_t))); if (bufferLen < sizeof(RPCIDHead) + sizeof(uint32_t) + sysCapLength) { PRINT_ERR("Parse file failed(SysCap length exceeded), input file : %s\n", inputFile); + FreeContextBuffer(contextBuffer); return -1; } diff --git a/src/create_pcid.c b/src/create_pcid.c index 4972ac92879591ab66fb310e38c6c5751389d63f..ef2764f9b8b4e755e6fc669bd3a383ff7eae0715 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -25,6 +25,7 @@ #include "endian_internal.h" #include "create_pcid.h" #include "context_tool.h" +#include "common_method.h" #ifdef SYSCAP_DEFINE_EXTERN_ENABLE #include "syscap_define_custom.h" @@ -58,7 +59,6 @@ struct FreeEncodePcidInfo { char *outDirPathFinal; }; - struct FreeDecodePcidJsonInfo { char *strJson; char *contextBuffer; @@ -227,7 +227,7 @@ static int32_t CheckConvertedContextSaveAsFile(char *outDirPath, PCIDMain *pcidB } static int32_t FreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, char *contextBuffer, - int32_t type, int32_t ret) + int32_t type, int32_t ret) { if (type == FREE_CREATE_PCID_BUFFER_OUT) { free(pcidBuffer); @@ -238,7 +238,7 @@ static int32_t FreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, } static int32_t PreFreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, cJSON *jsonRootObj, - char *contextBuffer, int32_t type) + char *contextBuffer, int32_t type) { cJSON_Delete(jsonRootObj); return FreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, contextBuffer, type, -1); @@ -249,11 +249,10 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) uint32_t privateCapSize, osCapSize; uint32_t contextBufLen; char *contextBuffer = NULL; - cJSON *allOsSyscapObj = CreateWholeSyscapJsonObj(); - int32_t ret = GetFileContext(inputFile, &contextBuffer, (uint32_t *)&contextBufLen); + cJSON *allOsSyscapObj = CreateWholeSyscapJsonObj(); + int32_t ret = CheckFileAndGetFileContext(inputFile, &contextBuffer, (uint32_t *)&contextBufLen); if (ret != 0) { - PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, -1); } @@ -271,6 +270,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) cJSON *jsonOsSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "os"); cJSON *jsonPriSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "private"); + ret = GetOsAndPriSyscapSize(jsonOsSyscapObj, jsonPriSyscapObj, &osCapSize, &privateCapSize); if (ret != 0) { return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0); @@ -295,7 +295,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) ret += SetPCIDHeader(pcidBuffer, jsonRootObj); if (ret != 0) { return PreFreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, jsonRootObj, contextBuffer, - FREE_CREATE_PCID_BUFFER_OUT); + FREE_CREATE_PCID_BUFFER_OUT); } ret = CheckConvertedContextSaveAsFile(outDirPath, pcidBuffer, pcidLength, ret); @@ -358,7 +358,7 @@ static int32_t GetPriSyscapResult(cJSON *capVectorPtr, int32_t ret) return ret; } -int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufLen) +static int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, int32_t contextBufLen) { cJSON *capVectorPtr = cJSON_CreateArray(); if (capVectorPtr == NULL) { @@ -366,12 +366,7 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL return -1; } - if (contextBufLen > UINT32_MAX) { - PRINT_ERR("the data privateSyscapLen is out of scope."); - return GetPriSyscapResult(capVectorPtr, -1); - } - - int32_t privateSyscapLen = (int32_t)(contextBufLen - sizeof(PCIDMain) - 1); + int32_t privateSyscapLen = contextBufLen - sizeof(PCIDMain) - 1; if (privateSyscapLen < 0 || privateSyscapLen > INT32_MAX) { PRINT_ERR("parse private syscap failed."); return GetPriSyscapResult(capVectorPtr, -1); @@ -409,7 +404,7 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL } static int32_t CheckSysCapObj(struct FreeDecodePcidJsonInfo freePcidJsonInfo, PCIDMain *pcidMain, - uint32_t contextBufLen, int32_t ret) + uint32_t contextBufLen, int32_t ret) { if (freePcidJsonInfo.sysCapObj == NULL) { PRINT_ERR("cJSON_CreateObject failed\n"); @@ -452,9 +447,8 @@ static int32_t FreeAfterDecodePCID(struct FreeDecodePcidJsonInfo freePcidJsonInf { switch (type) { case FREE_DECODE_PCID_CONVERT_OUT: - free(freePcidJsonInfo.strJson); + cJSON_free(freePcidJsonInfo.strJson); cJSON_Delete(freePcidJsonInfo.jsonRootObj); - cJSON_Delete(freePcidJsonInfo.sysCapObj); FreeContextBuffer(freePcidJsonInfo.contextBuffer); break; case FREE_DECODE_PCID_ROOT_OUT: @@ -467,6 +461,9 @@ static int32_t FreeAfterDecodePCID(struct FreeDecodePcidJsonInfo freePcidJsonInf FreeContextBuffer(freePcidJsonInfo.contextBuffer); break; case FREE_DECODE_PCID_CONTEXT_OUT: + cJSON_Delete(freePcidJsonInfo.sysCapObj); + FreeContextBuffer(freePcidJsonInfo.contextBuffer); + break; default: FreeContextBuffer(freePcidJsonInfo.contextBuffer); } @@ -483,10 +480,9 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) freePcidJsonInfo.jsonRootObj = NULL; freePcidJsonInfo.sysCapObj = NULL; freePcidJsonInfo.flag = 0; - - ret = GetFileContext(inputFile, &freePcidJsonInfo.contextBuffer, (uint32_t *)&contextBufLen); + + ret = CheckFileAndGetFileContext(inputFile, &freePcidJsonInfo.contextBuffer, (uint32_t *)&contextBufLen); if (ret != 0) { - PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); return -1; } @@ -526,15 +522,14 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_ROOT_OUT, ret); } - freePcidJsonInfo.sysCapObj = NULL; // avoid being released repeatedly. + freePcidJsonInfo.strJson = cJSON_Print(freePcidJsonInfo.jsonRootObj); - char *strJson = cJSON_Print(freePcidJsonInfo.jsonRootObj); const char outputFileName[] = "pcid.json"; - ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, strJson, strlen(strJson)); + ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, freePcidJsonInfo.strJson, + strlen(freePcidJsonInfo.strJson)); if (ret != 0) { PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, outputFileName); } - free(strJson); return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONVERT_OUT, ret); } @@ -588,7 +583,7 @@ static int32_t AddHeaderToJsonObj(uint32_t *pcidHeader, uint32_t pcidHeaderLen, // trans system type to string char *systemType = header->systemType == 0b001 ? "mini" : (header->systemType == 0b010 ? "small" : - (header->systemType == 0b100 ? "standard" : NULL)); + (header->systemType == 0b100 ? "standard" : NULL)); if (systemType == NULL) { PRINT_ERR("prase system type failed.\n"); return -1; @@ -665,7 +660,6 @@ static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapSt cJSON *sysCapArray = cJSON_CreateArray(); if (sysCapArray == NULL) { PRINT_ERR("Create cJSON array failed.\n"); - free(sysCapArray); return -1; } if (priSyscapStringLen == 0) { @@ -697,11 +691,14 @@ static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapSt static int32_t GetSyscapStr(char *input, char const *priSyscapStr, uint32_t* osSyscap, uint32_t *pcidHeader) { + if (input == NULL) { + PRINT_ERR("inputFile is null.\n"); + return -1; + } char *ctx = NULL; uint32_t fileContextLen; if (GetFileContext(input, &ctx, (uint32_t *)&fileContextLen) != 0) { PRINT_ERR("GetFileContext failed, input file : %s\n", input); - free(ctx); return -1; } if (ParseStringSyscap(ctx, osSyscap, OS_SYSCAP_NUM, pcidHeader, PCID_HEADER) != 0) { @@ -719,6 +716,8 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) uint32_t osSyscap[OS_SYSCAP_NUM] = {0}; uint32_t pcidHeader[PCID_HEADER]; char *priSyscapStr = NULL; + char *jsonBuffer = NULL; + ret = GetSyscapStr(input, priSyscapStr, osSyscap, pcidHeader); if (ret == -1) { return ret; @@ -727,40 +726,44 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) // add to json object cJSON *sysCapObj = cJSON_CreateObject(); cJSON *rootObj = cJSON_CreateObject(); + if (sysCapObj == NULL || rootObj == NULL) { + PRINT_ERR("Failed to create cJSON objects.\n"); + goto FAILED; + } + if (!cJSON_AddItemToObject(rootObj, "syscap", sysCapObj)) { PRINT_ERR("Add syscap to json failed.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } if (AddHeaderToJsonObj(pcidHeader, PCID_HEADER, rootObj) != 0) { PRINT_ERR("Add header to json object failed.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } if (AddOsSyscapToJsonObj(osSyscap, OS_SYSCAP_NUM, sysCapObj) != 0) { PRINT_ERR("Add os syscap json object failed.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } - if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t)strlen(priSyscapStr), sysCapObj) != 0) { + if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t) strlen(priSyscapStr), sysCapObj) != 0) { PRINT_ERR("Add private syscap json object failed.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } // save as json file - char *jsonBuffer = cJSON_Print(rootObj); + jsonBuffer = cJSON_Print(rootObj); if (jsonBuffer == NULL) { PRINT_ERR("json buffer is null.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } const char outputFileName[] = "pcid.json"; - if (ConvertedContextSaveAsFile(outDirPath, outputFileName, - jsonBuffer, strlen(jsonBuffer)) != 0) { + if (ConvertedContextSaveAsFile(outDirPath, outputFileName, jsonBuffer, strlen(jsonBuffer)) != 0) { PRINT_ERR("Save as json file failed.\n"); - goto SAVE_FAILED; + goto FAILED; } ret = 0; -SAVE_FAILED: - free(jsonBuffer); -ADD_JSON_FAILED: +FAILED: + cJSON_free(jsonBuffer); + SafeFree(priSyscapStr); cJSON_Delete(sysCapObj); cJSON_Delete(rootObj); return ret; @@ -786,7 +789,7 @@ static int32_t FreeAfterEncodePCID(struct FreeEncodePcidInfo freePcidInfo, int32 } static int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapLen, uint32_t *mainSyscap, - struct FreeEncodePcidInfo freePcidInfo, int32_t ret) + struct FreeEncodePcidInfo freePcidInfo, int32_t ret) { // 17, size of "SystemCapability." uint32_t outputLen = U32_TO_STR_MAX_LEN * PCID_OUT_BUFFER + 17 * priSyscapCount + privateSyscapLen + 1; @@ -832,7 +835,7 @@ static int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapL } static int32_t CheckPrivateSyCap(struct FreeEncodePcidInfo freePcidInfo, uint32_t privateSyscapLen, - char *privateSyscap, int32_t ret) + char *privateSyscap, int32_t ret) { uint32_t i, j; char tempSyscap[SINGLE_SYSCAP_LEN] = {0}; @@ -872,9 +875,8 @@ int32_t EncodePcidscToString(char *inputFile, char *outDirPath) freePcidInfo.outDirPathFinal = outDirPath; PCIDMain *pcidMain = NULL; - ret = GetFileContext(inputFile, &freePcidInfo.contextBuffer, (uint32_t *)&bufferLen); + ret = CheckFileAndGetFileContext(inputFile, &freePcidInfo.contextBuffer, (uint32_t *)&bufferLen); if (ret != 0) { - PRINT_ERR("Get pcid file failed, pcid file path: %s\n", inputFile); return -1; } diff --git a/src/syscap_tool.c b/src/syscap_tool.c index b2ab4fdb4581cd1dbd7cd0b19f4e2d5e44e130a7..89f7ae1a6c039b47433238c141f831c40b51673c 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -27,6 +27,7 @@ #include "create_pcid.h" #include "syscap_tool.h" #include "context_tool.h" +#include "common_method.h" #ifdef SYSCAP_DEFINE_EXTERN_ENABLE #include "syscap_define_custom.h" @@ -71,6 +72,14 @@ struct FreeAfterEncodeRpcidscInfo { int16_t flag; }; +static void FreePointerMemory(char *pcidContent, char *rpcidContent, char *pcidPriSyscap, char *rpcidPriSyscap) +{ + SafeFree(pcidContent); + SafeFree(rpcidPriSyscap); + SafeFree(rpcidContent); + SafeFree(pcidPriSyscap); +} + static int32_t FillOsCapLength(char *convertedBuffer, char *contextBuffer, struct JsonObjectSysCap gJsonObjectSysCap, uint32_t sysCapSize, int32_t ret) { @@ -141,8 +150,7 @@ int32_t RPCIDEncode(char *inputFile, char *outputPath) gJsonObjectSysCap.cjsonObjectRoot = NULL; gJsonObjectSysCap.sysCapPtr = NULL; - if (GetFileContext(inputFile, &contextBuffer, &bufferLen) != 0) { - PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); + if (CheckFileAndGetFileContext(inputFile, &contextBuffer, &bufferLen) != 0) { return -1; } @@ -204,6 +212,11 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t); RPCIDHead *rpcidHeader = (RPCIDHead *)input; cJSON *sysCapJson = cJSON_CreateArray(); + if (sysCapJson == NULL) { + PRINT_ERR("Failed to create sysCapJson array\n"); + return -1; + } + for (i = 0; i < sysCapCount; i++) { char *temp = sysCapBegin + i * SINGLE_FEAT_LEN; if (strlen(temp) >= SINGLE_FEAT_LEN || strlen(temp) == 0) { @@ -248,6 +261,11 @@ int32_t RPCIDDecode(char *inputFile, char *outputPath) char *convertedBuffer = NULL; uint32_t bufferLen; + if (inputFile == NULL) { + PRINT_ERR("inputFile is null.\n"); + return -1; + } + // check rpcid.sc if (CheckRpcidFormat(inputFile, &contextBuffer, &bufferLen)) { PRINT_ERR("Check rpcid.sc format failed. Input failed: %s\n", inputFile); @@ -263,14 +281,18 @@ int32_t RPCIDDecode(char *inputFile, char *outputPath) // save to json file convertedBuffer = cJSON_Print(rpcidRoot); + if (convertedBuffer == NULL) { + PRINT_ERR("cJSON_Print failed to create JSON string\n"); + goto FREE_RPCID_ROOT; + } + ret = ConvertedContextSaveAsFile(outputPath, "rpcid.json", convertedBuffer, strlen(convertedBuffer)); if (ret != 0) { PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.json\n", outputPath); - free(convertedBuffer); + cJSON_free(convertedBuffer); goto FREE_RPCID_ROOT; } - free(convertedBuffer); - + cJSON_free(convertedBuffer); FREE_RPCID_ROOT: cJSON_Delete(rpcidRoot); FREE_CONTEXT_OUT: @@ -462,6 +484,11 @@ int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath) freeAfterEncodeRpcidscInfo.type = 0; freeAfterEncodeRpcidscInfo.flag = 0; + if (inputFile == NULL) { + PRINT_ERR("inputFile is null.\n"); + return -1; + } + // check rpcid.sc if (CheckRpcidFormat(inputFile, &freeAfterEncodeRpcidscInfo.contextBuffer, &bufferLen) != 0) { PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile); @@ -681,6 +708,11 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ uint32_t pcidOsArray[PCID_OUT_BUFFER] = {0}; uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0}; + if (pcidFile == NULL || rpcidFile == NULL) { + PRINT_ERR("pcidFile or rpcidFile is null.\n"); + return -1; + } + if (type == TYPE_FILE) { if (GetFileContext(pcidFile, &pcidContent, &pcidContentLen)) { PRINT_ERR("Get pcid file context failed, input file : %s\n", pcidFile); @@ -705,10 +737,7 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ &rpcidPriSyscap, &rpcidPriSyscapLen); if (ret != 0) { PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret); - free(pcidContent); - free(rpcidContent); - free(pcidPriSyscap); - free(rpcidPriSyscap); + FreePointerMemory(pcidContent, rpcidContent, pcidPriSyscap, rpcidPriSyscap); return -1; } @@ -720,5 +749,6 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ } else { printf("Fail! The pcid does not meet the rpcid\n"); } + return 0; } \ No newline at end of file diff --git a/test/unittest/common/BUILD.gn b/test/unittest/common/BUILD.gn index 668ac7ded2f1467ea3f9c6cee20268b6d330922e..262134296f2008458458a825b58e9adc52aff787 100644 --- a/test/unittest/common/BUILD.gn +++ b/test/unittest/common/BUILD.gn @@ -41,6 +41,7 @@ if (defined(ohos_lite)) { "../../../src/create_pcid.c", "../../../src/endian_internal.c", "../../../src/syscap_tool.c", + "../../../src/common_method.c", "syscap_codec_test.cpp", ] @@ -82,6 +83,7 @@ if (defined(ohos_lite)) { "../../../src/create_pcid.c", "../../../src/endian_internal.c", "../../../src/syscap_tool.c", + "../../../src/common_method.c", "syscap_codec_test.cpp", ] diff --git a/tools/syscap_collector.py b/tools/syscap_collector.py index 18ee661ba2099ae81ba212b89b7dae5f809a7bae..74c0f963146ee14ffe8b0b3eca255fca5e8fc175 100755 --- a/tools/syscap_collector.py +++ b/tools/syscap_collector.py @@ -208,7 +208,7 @@ def _check_path_prefix(paths): def traversal_files(subsystem_path, _files): for item in os.scandir(subsystem_path): - if is_symlik(item.path): + if is_symlink(item.path): continue elif item.is_file() and item.name == 'ohos.build': _files.append(item.path) @@ -230,7 +230,7 @@ def get_file_type(file_path): return 'unknown' -def is_symlik(file_path): +def is_symlink(file_path): file_type = get_file_type(file_path) if file_type == 'symlink': link_target = os.readlink(file_path)