From dbdd3e21bfce2bf380945cbbb13e48c9511ce4e2 Mon Sep 17 00:00:00 2001 From: yudechen Date: Thu, 10 Nov 2022 18:26:45 +0800 Subject: [PATCH 1/2] fix: modify for codecheck. Signed-off-by: yudechen Change-Id: I958ff1debd07586cdeee83e4732a4aefcfab4651 --- napi/napi_query_syscap.cpp | 48 +++++++++++++++++++------------------- src/create_pcid.c | 44 +++++++++++++++++----------------- src/syscap_tool.c | 26 ++++++++++----------- 3 files changed, 59 insertions(+), 59 deletions(-) diff --git a/napi/napi_query_syscap.cpp b/napi/napi_query_syscap.cpp index 5cbb3d9..858f934 100644 --- a/napi/napi_query_syscap.cpp +++ b/napi/napi_query_syscap.cpp @@ -42,7 +42,7 @@ constexpr size_t KEY_BUFFER_SIZE = 32; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data) // Async Function Set -struct systemCapabilityAsyncContext { +struct SystemCapabilityAsyncContext { napi_env env = nullptr; napi_async_work work = nullptr; char key[KEY_BUFFER_SIZE] = { 0 }; @@ -64,7 +64,7 @@ static char* getSystemCapability() uint32_t *osCapU32 = nullptr; char *priOutput = nullptr; char *temp = nullptr; - char *allSyscapBUffer = nullptr; + char *allSyscapBuffer = nullptr; char osCapArray[PCID_MAIN_U32][U32_TO_STR_MAX_LEN] = {}; char (*priCapArray)[SINGLE_SYSCAP_LEN] = nullptr; @@ -105,39 +105,39 @@ static char* getSystemCapability() sumLen += (PCID_MAIN_U32 + priCapArrayCnt + 1); // split with ',' // splicing string - allSyscapBUffer = (char *)malloc(sumLen); - if (allSyscapBUffer == nullptr) { + allSyscapBuffer = (char *)malloc(sumLen); + if (allSyscapBuffer == nullptr) { PRINT_ERR("malloc failed!"); goto FREE_PRICAP_ARRAY; } - err = memset_s(allSyscapBUffer, sumLen, 0, sumLen); + err = memset_s(allSyscapBuffer, sumLen, 0, sumLen); if (err != EOK) { PRINT_ERR("memset failed!"); - free(allSyscapBUffer); - allSyscapBUffer = nullptr; + free(allSyscapBuffer); + allSyscapBuffer = nullptr; goto FREE_PRICAP_ARRAY; } temp = *osCapArray; for (size_t i = 1; i < PCID_MAIN_U32; i++) { - retError = sprintf_s(allSyscapBUffer, sumLen, "%s,%s", temp, osCapArray[i]); + retError = sprintf_s(allSyscapBuffer, sumLen, "%s,%s", temp, osCapArray[i]); if (retError == -1) { PRINT_ERR("splicing os syscap string failed."); - free(allSyscapBUffer); - allSyscapBUffer = nullptr; + free(allSyscapBuffer); + allSyscapBuffer = nullptr; goto FREE_PRICAP_ARRAY; } - temp = allSyscapBUffer; + temp = allSyscapBuffer; } for (int i = 0; i < priCapArrayCnt; i++) { - retError = sprintf_s(allSyscapBUffer, sumLen, "%s,%s", temp, *(priCapArray + i)); + retError = sprintf_s(allSyscapBuffer, sumLen, "%s,%s", temp, *(priCapArray + i)); if (retError == -1) { PRINT_ERR("splicing pri syscap string failed."); - free(allSyscapBUffer); - allSyscapBUffer = nullptr; + free(allSyscapBuffer); + allSyscapBuffer = nullptr; goto FREE_PRICAP_ARRAY; } - temp = allSyscapBUffer; + temp = allSyscapBuffer; } FREE_PRICAP_ARRAY: @@ -145,7 +145,7 @@ FREE_PRICAP_ARRAY: FREE_PRIOUTPUT: free(priOutput); - return allSyscapBUffer; + return allSyscapBuffer; } napi_value QuerySystemCapability(napi_env env, napi_callback_info info) @@ -154,7 +154,7 @@ napi_value QuerySystemCapability(napi_env env, napi_callback_info info) NAPI_ASSERT(env, argc <= 1, "too many parameters"); napi_value result = nullptr; - systemCapabilityAsyncContext* asyncContext = new systemCapabilityAsyncContext(); + SystemCapabilityAsyncContext* asyncContext = new SystemCapabilityAsyncContext(); asyncContext->env = env; @@ -178,7 +178,7 @@ napi_value QuerySystemCapability(napi_env env, napi_callback_info info) napi_create_async_work( env, nullptr, resource, [](napi_env env, void* data) { - systemCapabilityAsyncContext *asyncContext = (systemCapabilityAsyncContext *)data; + SystemCapabilityAsyncContext *asyncContext = (SystemCapabilityAsyncContext *)data; char *syscapStr = getSystemCapability(); if (syscapStr != nullptr) { asyncContext->value = syscapStr; @@ -188,8 +188,8 @@ napi_value QuerySystemCapability(napi_env env, napi_callback_info info) } }, [](napi_env env, napi_status status, void* data) { - systemCapabilityAsyncContext *asyncContext = (systemCapabilityAsyncContext *)data; - napi_value result[2] = {0}; + SystemCapabilityAsyncContext *asyncContext = (SystemCapabilityAsyncContext *)data; + napi_value result[2] = {nullptr, nullptr}; if (!asyncContext->status) { napi_get_undefined(env, &result[0]); napi_create_string_utf8(env, asyncContext->value, strlen(asyncContext->value), &result[1]); // ? @@ -235,14 +235,14 @@ EXTERN_C_END /* * Module define */ -static napi_module systemCapabilityModule = { +static napi_module g_systemCapabilityModule = { .nm_version = 1, .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = QuerryExport, .nm_modname = "systemCapability", - .nm_priv = ((void*)0), - .reserved = {0}, + .nm_priv = nullptr, + .reserved = {nullptr}, }; /* @@ -250,6 +250,6 @@ static napi_module systemCapabilityModule = { */ extern "C" __attribute__((constructor)) void systemCapabilityRegisterModule(void) { - napi_module_register(&systemCapabilityModule); + napi_module_register(&g_systemCapabilityModule); } } \ No newline at end of file diff --git a/src/create_pcid.c b/src/create_pcid.c index 50feb86..a495a0b 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -231,8 +231,8 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) uint16_t allPriSyscapStrLen = 0; for (i = 0; i < privateCapSize; i++) { - jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, i); - allPriSyscapStrLen += strlen(strchr(jsonArrayItem->valuestring, '.') + 1); + jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, (int)i); + allPriSyscapStrLen += (uint16_t)strlen(strchr(jsonArrayItem->valuestring, '.') + 1); allPriSyscapStrLen++; // for separator ',' } if ((allPriSyscapStrLen + 1) > PRIVATE_SYSCAP_SIZE) { @@ -241,18 +241,18 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) goto FREE_CONVERT_OUT; } - uint16_t PCIDLength = sizeof(PCIDMain) + allPriSyscapStrLen + 1; - PCIDMain *PCIDBuffer = (PCIDMain *)malloc(PCIDLength); - if (PCIDBuffer == NULL) { + uint16_t pcidLength = sizeof(PCIDMain) + allPriSyscapStrLen + 1; + PCIDMain *pcidBuffer = (PCIDMain *)malloc(pcidLength); + if (pcidBuffer == NULL) { PRINT_ERR("malloc for pcid buffer failed\n"); ret = -1; goto FREE_CONVERT_OUT; } - (void)memset_s(PCIDBuffer, PCIDLength, 0, PCIDLength); + (void)memset_s(pcidBuffer, pcidLength, 0, pcidLength); // process os syscap for (i = 0; i < osCapSize; i++) { - jsonArrayItem = cJSON_GetArrayItem(jsonOsSyscapObj, i); + jsonArrayItem = cJSON_GetArrayItem(jsonOsSyscapObj, (int)i); osCapIndex = cJSON_GetObjectItem(allOsSyscapObj, jsonArrayItem->valuestring); if (osCapIndex == NULL) { PRINT_ERR("can't find the syscap: %s, please add it in syscap_define.h.\n", jsonArrayItem->valuestring); @@ -266,14 +266,14 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) ret = -1; goto FREE_PCID_BUFFER_OUT; } - PCIDBuffer->osSyscap[sectorOfBits] |= 1 << (posOfBits); + pcidBuffer->osSyscap[sectorOfBits] |= 1 << (posOfBits); } // process private syscap - char *priSyscapHead = (char *)(PCIDBuffer + 1); + char *priSyscapHead = (char *)(pcidBuffer + 1); char *priSyscapStr = NULL; for (i = 0; i < privateCapSize; i++) { - jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, i); + jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, (int)i); priSyscapStr = strchr(jsonArrayItem->valuestring, '.') + 1; nRet = strcat_s(priSyscapHead, allPriSyscapStrLen + 1, priSyscapStr); nRet += strcat_s(priSyscapHead, allPriSyscapStrLen + 1, ","); @@ -290,8 +290,8 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) ret = -1; goto FREE_PCID_BUFFER_OUT; } - PCIDBuffer->apiVersion = HtonsInter((uint16_t)jsonSyscapObj->valueint); - PCIDBuffer->apiVersionType = 0; + pcidBuffer->apiVersion = HtonsInter((uint16_t)jsonSyscapObj->valueint); + pcidBuffer->apiVersionType = 0; jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "system_type"); if (jsonSyscapObj == NULL || !cJSON_IsString(jsonSyscapObj)) { @@ -300,10 +300,10 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) goto FREE_PCID_BUFFER_OUT; } systemType = jsonSyscapObj->valuestring; - PCIDBuffer->systemType = !strcmp(systemType, "mini") ? 0b001 : + pcidBuffer->systemType = !strcmp(systemType, "mini") ? 0b001 : (!strcmp(systemType, "small") ? 0b010 : (!strcmp(systemType, "standard") ? 0b100 : 0)); - if (PCIDBuffer->systemType == 0) { + if (pcidBuffer->systemType == 0) { PRINT_ERR("\"system_type\" is invaild, systemType = \"%s\"\n", systemType); ret = -1; goto FREE_PCID_BUFFER_OUT; @@ -315,10 +315,10 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) ret = -1; goto FREE_PCID_BUFFER_OUT; } - PCIDBuffer->manufacturerID = HtonlInter((uint32_t)jsonSyscapObj->valueint); + pcidBuffer->manufacturerID = HtonlInter((uint32_t)jsonSyscapObj->valueint); const char pcidFileName[] = "PCID.sc"; - ret = ConvertedContextSaveAsFile(outDirPath, pcidFileName, (char *)PCIDBuffer, PCIDLength); + ret = ConvertedContextSaveAsFile(outDirPath, pcidFileName, (char *)pcidBuffer, pcidLength); if (ret != 0) { PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, pcidFileName); ret = -1; @@ -326,7 +326,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) } FREE_PCID_BUFFER_OUT: - free(PCIDBuffer); + free(pcidBuffer); FREE_CONVERT_OUT: free(allOsSyscapObj); FreeContextBuffer(contextBuffer); @@ -376,7 +376,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) } nRet = memcpy_s(osSyscap, OS_SYSCAP_BYTES, (uint8_t *)pcidMain + 8, OS_SYSCAP_BYTES); // 8, bytes of pcid header - if (EOK != nRet) { + if (nRet != EOK) { PRINT_ERR("memcpy_s failed."); ret = -1; goto FREE_VECTOR_OUT; @@ -420,7 +420,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) } char *ptrPrivateSyscap = (char *)(pcidMain + 1); - size_t privateSyscapLen = contextBufLen - sizeof(PCIDMain) - 1; + int privateSyscapLen = (int)(contextBufLen - sizeof(PCIDMain) - 1); char priSyscapStr[SINGLE_SYSCAP_LEN] = {0}; char *tempPriSyscapStr = priSyscapStr; char fullPriSyscapStr[SINGLE_SYSCAP_LEN] = {0}; @@ -694,15 +694,15 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) PRINT_ERR("Add syscap to json failed.\n"); goto ADD_JSON_FAILED; } - if (AddHeaderToJsonObj(pcidHeader, PCID_HEADER, rootObj)) { + if (AddHeaderToJsonObj(pcidHeader, PCID_HEADER, rootObj) != 0) { PRINT_ERR("Add header to json object failed.\n"); goto ADD_JSON_FAILED; } - if (AddOsSyscapToJsonObj(osSyscapUintArray, OS_SYSCAP_NUM, sysCapObj)) { + if (AddOsSyscapToJsonObj(osSyscapUintArray, OS_SYSCAP_NUM, sysCapObj) != 0) { PRINT_ERR("Add os syscap json object failed.\n"); goto ADD_JSON_FAILED; } - if (AddPriSyscapToJsonObj(priSyscapStr, strlen(priSyscapStr), sysCapObj)) { + if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t)strlen(priSyscapStr), sysCapObj) != 0) { PRINT_ERR("Add private syscap json object failed.\n"); goto ADD_JSON_FAILED; } diff --git a/src/syscap_tool.c b/src/syscap_tool.c index 0765f17..ff3e461 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -170,7 +170,7 @@ static cJSON *CreateWholeSyscapJsonObj(void) return root; } -int32_t RPCIDEncode(char *inputFile, char *outDirPath) +int32_t RPCIDEncode(char *inputFile, char *outputPath) { int32_t ret; char *contextBuffer = NULL; @@ -240,7 +240,7 @@ int32_t RPCIDEncode(char *inputFile, char *outDirPath) *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(sysCapSize * SINGLE_FEAT_LEN)); fillTmpPtr += sizeof(uint16_t); for (uint32_t i = 0; i < sysCapSize; i++) { - arrayItemPtr = cJSON_GetArrayItem(sysCapPtr, i); + arrayItemPtr = cJSON_GetArrayItem(sysCapPtr, (int)i); char *pointPos = strchr(arrayItemPtr->valuestring, '.'); if (pointPos == NULL) { PRINT_ERR("context of \"syscap\" array is invalid\n"); @@ -263,9 +263,9 @@ int32_t RPCIDEncode(char *inputFile, char *outDirPath) fillTmpPtr += SINGLE_FEAT_LEN; } - ret = ConvertedContextSaveAsFile(outDirPath, "RPCID.sc", convertedBuffer, convertedBufLen); + ret = ConvertedContextSaveAsFile(outputPath, "RPCID.sc", convertedBuffer, convertedBufLen); if (ret != 0) { - PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:rpcid.sc\n", outDirPath); + PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.sc\n", outputPath); goto FREE_CONVERT_OUT; } @@ -360,7 +360,7 @@ static int32_t CheckRpcidFormat(char *inputFile, char **Buffer, uint32_t *Len) return 0; } -int32_t RPCIDDecode(char *inputFile, char *outDirPath) +int32_t RPCIDDecode(char *inputFile, char *outputPath) { int32_t ret = 0; char *contextBuffer = NULL; @@ -375,16 +375,16 @@ int32_t RPCIDDecode(char *inputFile, char *outDirPath) // parse rpcid to json cJSON *rpcidRoot = cJSON_CreateObject(); - if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot)) { + if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot) != 0) { PRINT_ERR("Prase rpcid to json failed. Input failed: %s\n", inputFile); goto FREE_RPCID_ROOT; } // save to json file convertedBuffer = cJSON_Print(rpcidRoot); - ret = ConvertedContextSaveAsFile(outDirPath, "RPCID.json", convertedBuffer, strlen(convertedBuffer)); + ret = ConvertedContextSaveAsFile(outputPath, "RPCID.json", convertedBuffer, strlen(convertedBuffer)); if (ret != 0) { - PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:rpcid.json\n", outDirPath); + PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.json\n", outputPath); goto FREE_RPCID_ROOT; } @@ -440,7 +440,7 @@ int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath) // parse rpcid to json rpcidRoot = cJSON_CreateObject(); - if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot)) { + if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot) != 0) { PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile); goto FREE_RPCID_ROOT; } @@ -466,17 +466,17 @@ int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath) (void)memset_s(osSysCapIndex, sizeof(uint16_t) * sysCapArraySize, 0, sizeof(uint16_t) * sysCapArraySize); // malloc for save private syscap string - priSyscapArray = (char *)malloc(sysCapArraySize * SINGLE_SYSCAP_LEN); + priSyscapArray = (char *)malloc((uint32_t)sysCapArraySize * SINGLE_SYSCAP_LEN); if (priSyscapArray == NULL) { PRINT_ERR("malloc(%d) failed.\n", sysCapArraySize * SINGLE_SYSCAP_LEN); goto FREE_MALLOC_OSSYSCAP; } - (void)memset_s(priSyscapArray, sysCapArraySize * SINGLE_SYSCAP_LEN, - 0, sysCapArraySize * SINGLE_SYSCAP_LEN); + (void)memset_s(priSyscapArray, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN), + 0, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN)); priSyscap = priSyscapArray; // part os syscap and ptivate syscap for (i = 0; i < (uint32_t)sysCapArraySize; i++) { - cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i); + cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, (int)i); cJsonTemp = cJSON_GetObjectItem(sysCapDefine, cJsonItem->valuestring); if (cJsonTemp != NULL) { osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint); -- Gitee From 9a13d1bbb22692a3797f760350b5abca0ccbecfd Mon Sep 17 00:00:00 2001 From: yudechen Date: Thu, 17 Nov 2022 15:25:33 +0800 Subject: [PATCH 2/2] fix: fix some defects. Signed-off-by: yudechen Change-Id: Ifcba58db1dfe58e22aa652abc5399d8e24e5413b --- src/syscap_tool.c | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/syscap_tool.c b/src/syscap_tool.c index ff3e461..54193ff 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -40,6 +40,7 @@ typedef struct RequiredProductCompatibilityIDHead { #define PCID_OUT_BUFFER RPCID_OUT_BUFFER #define BYTES_OF_OS_SYSCAP 120 #define U32_TO_STR_MAX_LEN 11 +#define STRING_FORMAT_LEN_MAX 1024 #define PRINT_ERR(...) \ do { \ @@ -115,12 +116,12 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename char path[PATH_MAX + 1] = {0x00}; #ifdef _POSIX_ - if (strlen(outDirPath) > PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) { + if (strlen(outDirPath) >= PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) { PRINT_ERR("get path(%s) failed\n", outDirPath); return -1; } #else - if (strlen(outDirPath) > PATH_MAX || realpath(outDirPath, path) == NULL) { + if (strlen(outDirPath) >= PATH_MAX || realpath(outDirPath, path) == NULL) { PRINT_ERR("get file(%s) real path failed\n", outDirPath); return -1; } @@ -162,9 +163,9 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename static cJSON *CreateWholeSyscapJsonObj(void) { - size_t numOfSyscapAll = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); + size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); cJSON *root = cJSON_CreateObject(); - for (size_t i = 0; i < numOfSyscapAll; i++) { + for (size_t i = 0; i < allSyscapNum; i++) { cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num)); } return root; @@ -281,6 +282,10 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson uint32_t i; int32_t ret = 0; uint16_t sysCapLength = NtohsInter(*(uint16_t *)(input + sizeof(uint32_t))); + if (sysCapLength > inputLen - sizeof(uint32_t)) { + PRINT_ERR("Get sysCapLength(%u) error, inputLen = %u\n", sysCapLength, inputLen); + return -1; + } uint16_t sysCapCount = sysCapLength / SINGLE_FEAT_LEN; char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t); RPCIDHead *rpcidHeader = (RPCIDHead *)input; @@ -421,7 +426,6 @@ int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath) int32_t ret = 0; int32_t sysCapArraySize; uint32_t bufferLen, i; - uint16_t indexOs = 0; uint16_t indexPri = 0; uint16_t *osSysCapIndex; char *contextBuffer = NULL; @@ -475,8 +479,9 @@ int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath) 0, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN)); priSyscap = priSyscapArray; // part os syscap and ptivate syscap - for (i = 0; i < (uint32_t)sysCapArraySize; i++) { - cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, (int)i); + uint16_t indexOs = 0; + for (int i = 0; i < sysCapArraySize; i++) { + cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i); cJsonTemp = cJSON_GetObjectItem(sysCapDefine, cJsonItem->valuestring); if (cJsonTemp != NULL) { osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint); @@ -557,7 +562,7 @@ int32_t SeparateSyscapFromString(const char *inputString, uint32_t *osArray, uin { int32_t ret = 0; uint32_t i; - size_t inputLen = strlen(inputString); + uint32_t count = 0; char *temp = NULL; char *tok = NULL; @@ -568,7 +573,16 @@ int32_t SeparateSyscapFromString(const char *inputString, uint32_t *osArray, uin } // copy to temp string input - char *input = (char *)malloc(strlen(inputString) + 1); + if (inputString == NULL) { + PRINT_ERR("inputString is null.\n"); + return -1; + } + size_t inputLen = strlen(inputString); + if (inputLen > STRING_FORMAT_LEN_MAX) { + PRINT_ERR("input string too long(%zu).\n", inputLen); + return -1; + } + char *input = (char *)malloc(inputLen + 1); if (input == NULL) { PRINT_ERR("malloc failed.\n"); return -1; @@ -650,6 +664,7 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ bool priSysFound; uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0}; uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0}; + const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); if (type == TYPE_FILE) { if (GetFileContext(pcidFile, &pcidContent, &pcidContentLen)) { @@ -692,9 +707,13 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ continue; } for (uint8_t k = 0; k < INT_BIT; k++) { - if (temp2 & (0x1 << k)) { - // 2, header of pcid & rpcid - printf("Missing: %s\n", g_arraySyscap[(i - 2) * INT_BIT + k].str); + if (!(temp2 & (0x1 << k))) { + continue; + } + // 2, header of pcid & rpcid + size_t pos = (size_t)((i - 2) * INT_BIT + k); + if (pos < allSyscapNum) { + printf("Missing: %s\n", g_arraySyscap[pos].str); ossyscapFlag += 1; } } -- Gitee