diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index 9e1dff55be5c0c3a215144a8c0d8f771b3263391..1a70a9677bcaabf1a82bf5ded3539cd461cf2ea9 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -246,7 +246,6 @@ bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *o *bufferPos = '\0'; if (sprintf_s(*outputArray, SINGLE_SYSCAP_LEN, "SystemCapability.%s", buffer) == -1) { free(outputArray); - free(output); return false; } bufferPos = buffer; @@ -330,23 +329,23 @@ FREE_SYSCAP_OUT: static int32_t TransStringFormatAndSaveSyscap(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, cJSON *sysCapArray, const char *inputFile) { - int32_t ret = -1; // trans to string format sysCapArray = cJSON_GetObjectItem(freeAfterDecodeRpcidInfo.rpcidRoot, "syscap"); if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) { PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile); - return ret; + return -1; } freeAfterDecodeRpcidInfo.sysCapArraySize = cJSON_GetArraySize(sysCapArray); if (freeAfterDecodeRpcidInfo.sysCapArraySize < 0) { PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile); - return ret; + return -1; } // malloc for save os syscap index freeAfterDecodeRpcidInfo.osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) * freeAfterDecodeRpcidInfo.sysCapArraySize); if (freeAfterDecodeRpcidInfo.osSysCapIndex == NULL) { PRINT_ERR("malloc failed.\n"); + return -1; } free(freeAfterDecodeRpcidInfo.osSysCapIndex); return 0; diff --git a/src/create_pcid.c b/src/create_pcid.c index da181d8c668269882391a4c82aa6b1aa526286a3..d08ff36f5f0f1364760394dcffc74320c39510e8 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -222,7 +222,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); @@ -232,6 +232,13 @@ static int32_t FreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, return ret; } +static int32_t PreFreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, cJSON *jsonRootObj, + char *contextBuffer, int32_t type) +{ + cJSON_Delete(jsonRootObj); + return FreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, contextBuffer, type, -1); +} + int32_t CreatePCID(char *inputFile, char *outDirPath) { uint32_t privateCapSize, osCapSize; @@ -248,37 +255,33 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) cJSON *jsonRootObj = cJSON_ParseWithLength(contextBuffer, contextBufLen); if (jsonRootObj == NULL) { PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer); - return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, -1); + return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0); } cJSON *jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "syscap"); if (jsonSyscapObj == NULL || !cJSON_IsObject(jsonSyscapObj)) { PRINT_ERR("get \"syscap\" object failed\n"); - cJSON_Delete(jsonRootObj); - return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, -1); + return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0); } cJSON *jsonOsSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "os"); cJSON *jsonPriSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "private"); ret = GetOsAndPriSyscapSize(jsonOsSyscapObj, jsonPriSyscapObj, &osCapSize, &privateCapSize); if (ret != 0) { - cJSON_Delete(jsonRootObj); - return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, ret); + return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0); } uint16_t allPriSyscapStrLen = 0; ret = GetPriSyscapLen(privateCapSize, jsonPriSyscapObj, &allPriSyscapStrLen); if (ret != 0) { - cJSON_Delete(jsonRootObj); - return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, ret); + return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0); } uint16_t pcidLength = sizeof(PCIDMain) + allPriSyscapStrLen + 1; PCIDMain *pcidBuffer = (PCIDMain *)malloc(pcidLength); if (pcidBuffer == NULL) { PRINT_ERR("malloc for pcid buffer failed\n"); - cJSON_Delete(jsonRootObj); - return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, -1); + return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0); } (void)memset_s(pcidBuffer, pcidLength, 0, pcidLength); @@ -286,8 +289,8 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) ret += SetPriSyscap(pcidBuffer, jsonPriSyscapObj, privateCapSize, allPriSyscapStrLen); ret += SetPCIDHeader(pcidBuffer, jsonRootObj); if (ret != 0) { - cJSON_Delete(jsonRootObj); - return FreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, contextBuffer, FREE_CREATE_PCID_BUFFER_OUT, ret); + return PreFreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, jsonRootObj, contextBuffer, + FREE_CREATE_PCID_BUFFER_OUT); } ret = CheckConvertedContextSaveAsFile(outDirPath, pcidBuffer, pcidLength, ret); @@ -342,6 +345,12 @@ int32_t GetOsSyscap(PCIDMain *pcidMain, cJSON *sysCapObject) return 0; } +static int32_t GetPriSyscapResult(cJSON* capVectorPtr, int32_t ret) +{ + cJSON_Delete(capVectorPtr); + return ret; +} + int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufLen) { cJSON *capVectorPtr = cJSON_CreateArray(); @@ -352,16 +361,15 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL if (contextBufLen < 0 || contextBufLen > UINT32_MAX) { PRINT_ERR("the data privateSyscapLen is out of scope."); - return -1; + return GetPriSyscapResult(capVectorPtr, -1); } int32_t privateSyscapLen = (int32_t)(contextBufLen - sizeof(PCIDMain) - 1); - if (privateSyscapLen < 0) { + if (privateSyscapLen < 0 || privateSyscapLen > INT32_MAX) { PRINT_ERR("parse private syscap failed."); - return -1; + return GetPriSyscapResult(capVectorPtr, -1); } else if (privateSyscapLen == 0) { - cJSON_Delete(capVectorPtr); - return 0; + return GetPriSyscapResult(capVectorPtr, 0); } char fullCapStr[SINGLE_SYSCAP_LEN] = {0}; @@ -374,13 +382,11 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL int32_t ret = sprintf_s(fullCapStr, SINGLE_SYSCAP_LEN, "SystemCapability.%s", priSyscapStr); if (ret == -1) { printf("sprintf_s failed\n"); - cJSON_Delete(capVectorPtr); - return -1; + return GetPriSyscapResult(capVectorPtr, -1); } if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(fullCapStr))) { printf("cJSON_AddItemToArray or cJSON_CreateString failed\n"); - cJSON_Delete(capVectorPtr); - return -1; + return GetPriSyscapResult(capVectorPtr, -1); } tempPriSyscapStr = priSyscapStr; ptrPrivateSyscap++; @@ -390,8 +396,7 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL } if (!cJSON_AddItemToObject(sysCapObject, "private", capVectorPtr)) { PRINT_ERR("cJSON_AddItemToObject failed\n"); - cJSON_Delete(capVectorPtr); - return -1; + return GetPriSyscapResult(capVectorPtr, -1); } return 0; } @@ -680,54 +685,64 @@ static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapSt cJSON_Delete(sysCapArray); return -1; } - cJSON_Delete(sysCapArray); return 0; } -int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) +static int32_t GetSyscapStr(char *input, char const *priSyscapStr, uint32_t* osSyscap, uint32_t *pcidHeader) { - int32_t ret = -1; - uint32_t osSyscap[OS_SYSCAP_NUM] = {0}; - uint32_t pcidHeader[PCID_HEADER]; - uint32_t fileContextLen; char *ctx = NULL; - char *priSyscapStr = NULL; - + uint32_t fileContextLen; if (GetFileContext(input, &ctx, (uint32_t *)&fileContextLen) != 0) { PRINT_ERR("GetFileContext failed, input file : %s\n", input); - goto PARSE_FAILED; + free(ctx); + return -1; } if (ParseStringSyscap(ctx, osSyscap, OS_SYSCAP_NUM, pcidHeader, PCID_HEADER) != 0) { PRINT_ERR("Parse string syscap failed.\n"); - goto PARSE_FAILED; + free(ctx); + return -1; } priSyscapStr = ctx; + free(ctx); + return 0; +} + +int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) +{ + int32_t ret = -1; + uint32_t osSyscap[OS_SYSCAP_NUM] = {0}; + uint32_t pcidHeader[PCID_HEADER]; + char *priSyscapStr = NULL; + ret = GetSyscapStr(input, priSyscapStr, osSyscap, pcidHeader); + if (ret == -1) { + return ret; + } // add to json object cJSON *sysCapObj = cJSON_CreateObject(); cJSON *rootObj = cJSON_CreateObject(); if (!cJSON_AddItemToObject(rootObj, "syscap", sysCapObj)) { PRINT_ERR("Add syscap to json failed.\n"); - cJSON_Delete(sysCapObj); goto ADD_JSON_FAILED; } if (AddHeaderToJsonObj(pcidHeader, PCID_HEADER, rootObj) != 0) { PRINT_ERR("Add header to json object failed.\n"); - cJSON_Delete(sysCapObj); goto ADD_JSON_FAILED; } if (AddOsSyscapToJsonObj(osSyscap, OS_SYSCAP_NUM, sysCapObj) != 0) { PRINT_ERR("Add os syscap json object failed.\n"); - cJSON_Delete(sysCapObj); goto ADD_JSON_FAILED; } if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t)strlen(priSyscapStr), sysCapObj) != 0) { PRINT_ERR("Add private syscap json object failed.\n"); - cJSON_Delete(sysCapObj); goto ADD_JSON_FAILED; } // save as json file char *jsonBuffer = cJSON_Print(rootObj); + if (jsonBuffer == NULL) { + PRINT_ERR("json buffer is null.\n"); + goto ADD_JSON_FAILED; + } const char outputFileName[] = "PCID.json"; if (ConvertedContextSaveAsFile(outDirPath, outputFileName, jsonBuffer, strlen(jsonBuffer)) != 0) { @@ -739,9 +754,8 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) SAVE_FAILED: free(jsonBuffer); ADD_JSON_FAILED: + cJSON_Delete(sysCapObj); cJSON_Delete(rootObj); -PARSE_FAILED: - free(ctx); return ret; } @@ -774,7 +788,6 @@ static int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapL output = (char *)malloc(outputLen); if (output == NULL) { PRINT_ERR("malloc failed\n"); - free(output); return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT, ret); } (void)memset_s(output, outputLen, 0, outputLen); diff --git a/src/main.c b/src/main.c index 47a776969a916afe7ff58cd1954c050eb0a43b07..388dc1df971b1f74cfadb8eec641b6970ce1c0e3 100644 --- a/src/main.c +++ b/src/main.c @@ -131,7 +131,8 @@ int32_t OperateByBitMap(char *const *argv, uint16_t bitMap, char *outputpath) PRINT_ERR("Input file too few or too many.\n"); return -1; } else { - (void)OutputVersion(argv[optind], optind); break; + (void)OutputVersion(argv[optind], optind); + break; } default: (void)OutputHelp(); diff --git a/src/syscap_tool.c b/src/syscap_tool.c index b79c6b7c7684b31aebf744d079bd1913848eb2b8..cc6051c2aab922c9f22004f6012f87438c6cf1dc 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -261,12 +261,13 @@ int32_t RPCIDDecode(char *inputFile, char *outputPath) ret = ConvertedContextSaveAsFile(outputPath, "RPCID.json", convertedBuffer, strlen(convertedBuffer)); if (ret != 0) { PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.json\n", outputPath); + free(convertedBuffer); goto FREE_RPCID_ROOT; } + free(convertedBuffer); FREE_RPCID_ROOT: cJSON_Delete(rpcidRoot); - free(convertedBuffer); FREE_CONTEXT_OUT: FreeContextBuffer(contextBuffer); return ret;