From 0309add38ce76711cef076028b8142f3fc7a2681 Mon Sep 17 00:00:00 2001 From: yudechen Date: Tue, 17 May 2022 16:08:15 +0800 Subject: [PATCH] fixed 1851fb9 from https://gitee.com/yudechen/developtools_syscap_codec/pulls/35 modify for codex. Signed-off-by: yudechen Change-Id: Id06b50afe31eb2dcd53271b2907bb3f33946b453 --- interfaces/inner_api/syscap_interface.c | 15 ++++++----- interfaces/inner_api/syscap_interface.h | 4 ++- napi/napi_query_syscap.cpp | 4 +-- src/create_pcid.c | 25 +++++++++-------- src/syscap_tool.c | 31 +++++++++++++--------- test/unittest/common/syscap_codec_test.cpp | 12 ++++----- 6 files changed, 52 insertions(+), 39 deletions(-) diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index d53dcc2..e3ffd83 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -25,10 +25,10 @@ #include "syscap_interface.h" #define PCID_OUT_BUFFER 32 -#define MAX_SYSCAP_STR_LEN 128 #define OS_SYSCAP_BYTES 120 #define BITS_OF_BYTE 8 #define PCID_MAIN_LEN 128 +#define PATH_MAX 4096 #define PRINT_ERR(...) \ do { \ @@ -43,12 +43,13 @@ static void FreeContextBuffer(char *contextBuffer) (void)free(contextBuffer); } -static uint32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) +static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) { - uint32_t ret; + int32_t ret; FILE *fp = NULL; struct stat statBuf; char *contextBuffer = NULL; + ret = stat(inputFile, &statBuf); if (ret != 0) { PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", inputFile, errno); @@ -63,6 +64,7 @@ static uint32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); return -1; } + fp = fopen(inputFile, "rb"); if (fp == NULL) { PRINT_ERR("open file(%s) failed, errno = %d\n", inputFile, errno); @@ -84,7 +86,7 @@ static uint32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) return 0; } -bool EncodeOsSyscap(char output[128]) +bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN], int length) { int32_t ret; int32_t res; @@ -130,7 +132,7 @@ bool EncodePrivateSyscap(char **output, int *outputLen) } (void)memset_s(outputStr, *outputLen, 0, *outputLen); - ret = strncpy_s(outputStr, *outputLen, contextBuffer + PCID_MAIN_LEN, *outputLen); + ret = strncpy_s(outputStr, *outputLen, contextBuffer + PCID_MAIN_LEN, *outputLen - 1); if (ret != 0) { PRINT_ERR("strcpy_s failed."); FreeContextBuffer(contextBuffer); @@ -197,8 +199,7 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt) { char (*outputArray)[MAX_SYSCAP_STR_LEN] = NULL; char *inputPos = input; - uint16_t bufferLen; - int ret; + int bufferLen, ret; int syscapCnt = 0; while (*inputPos != '\0') { diff --git a/interfaces/inner_api/syscap_interface.h b/interfaces/inner_api/syscap_interface.h index 1336887..6535a1f 100644 --- a/interfaces/inner_api/syscap_interface.h +++ b/interfaces/inner_api/syscap_interface.h @@ -25,7 +25,9 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -bool EncodeOsSyscap(char output[128]); +#define MAX_SYSCAP_STR_LEN 128 + +bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN], int length); bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt); bool EncodePrivateSyscap(char **output, int *outputLen); bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt); diff --git a/napi/napi_query_syscap.cpp b/napi/napi_query_syscap.cpp index bb403ff..4c012e6 100644 --- a/napi/napi_query_syscap.cpp +++ b/napi/napi_query_syscap.cpp @@ -61,7 +61,7 @@ static char* getSystemCapability() bool retBool; int retError, priOutputLen, priCapArrayCnt, sumLen; int i = 0; - char osOutput[128] = {}; + char osOutput[SYSCAP_STR_MAX_LEN] = {}; errno_t err = EOK; uint32_t *osCapU32 = nullptr; char *priOutput = nullptr; @@ -70,7 +70,7 @@ static char* getSystemCapability() char osCapArray[OS_SYSCAP_U32_NUM][U32_TO_STR_MAX_LEN] = {}; char (*priCapArray)[SYSCAP_STR_MAX_LEN] = nullptr; - retBool = EncodeOsSyscap(osOutput); + retBool = EncodeOsSyscap(osOutput, SYSCAP_STR_MAX_LEN); if (!retBool) { PRINT_ERR("get encoded os syscap failed."); return nullptr; diff --git a/src/create_pcid.c b/src/create_pcid.c index dc7d1dc..2e511c9 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -47,9 +47,9 @@ static void FreeContextBuffer(char *contextBuffer) (void)free(contextBuffer); } -static uint32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *contextBufLen) +static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *contextBufLen) { - uint32_t ret; + int32_t ret; FILE *fp = NULL; struct stat statBuf; char *contextBuffer = NULL; @@ -68,6 +68,7 @@ static uint32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t * PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); return -1; } + fp = fopen(inputFile, "rb"); if (fp == NULL) { PRINT_ERR("open file(%s) failed, errno = %d\n", inputFile, errno); @@ -107,6 +108,10 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename fileFullPath[pathLen] = '/'; } + if (strlen(fileFullPath) + strlen(filename) + 1 > PATH_MAX) { + PRINT_ERR("length of path too long.\n"); + return -1; + } ret = strncat_s(fileFullPath, PATH_MAX, filename, strlen(filename) + 1); if (ret != 0) { PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n", @@ -120,8 +125,7 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename return -1; } - ret = fwrite(convertedBuffer, contextBufLen, 1, fp); - if (ret != 1) { + if (fwrite(convertedBuffer, contextBufLen, 1, fp) != 1) { PRINT_ERR("can't write file(%s),errno = %d\n", fileFullPath, errno); (void)fclose(fp); return -1; @@ -144,9 +148,8 @@ static cJSON *CreateWholeSyscapJsonObj(void) int32_t CreatePCID(char *inputFile, char *outDirPath) { - uint8_t sectorOfBits, posOfBits; - int32_t ret, i; - uint32_t contextBufLen, osCapSize, privateCapSize; + int32_t ret, osCapSize, sectorOfBits, posOfBits; + uint32_t i, contextBufLen, privateCapSize; errno_t nRet = 0; char *contextBuffer = NULL; char *systemType = NULL; @@ -268,7 +271,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "system_type"); if (jsonSyscapObj == NULL || !cJSON_IsString(jsonSyscapObj)) { - PRINT_ERR("get \"system_type\" failed, jsonSyscapObj = %p\n", jsonSyscapObj); + PRINT_ERR("get \"system_type\" failed\n"); ret = -1; goto FREE_PCID_BUFFER_OUT; } @@ -308,12 +311,12 @@ FREE_CONVERT_OUT: int32_t DecodePCID(char *inputFile, char *outDirPath) { - int32_t ret, i, j; + int32_t ret; errno_t nRet = 0; char *contextBuffer = NULL; uint8_t osSyscap[BYTES_OF_OS_SYSCAP] = {0}; - uint16_t indexOfSyscap[960] = {0}; - uint32_t contextBufLen, countOfSyscap = 0; + uint16_t indexOfSyscap[BYTES_OF_OS_SYSCAP * BITS_OF_ONE_BYTE] = {0}; + uint32_t i, j, contextBufLen, countOfSyscap = 0; ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen); if (ret != 0) { diff --git a/src/syscap_tool.c b/src/syscap_tool.c index 4874ada..74c9882 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -54,9 +54,9 @@ static void FreeContextBuffer(char *contextBuffer) (void)free(contextBuffer); } -static uint32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *bufferLen) +static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *bufferLen) { - uint32_t ret; + int32_t ret; FILE *fp = NULL; struct stat statBuf; char *contextBuffer = NULL; @@ -113,6 +113,10 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, char *filename, char fileFullPath[pathLen] = '/'; } + if (strlen(filename) + 1 > PATH_MAX) { + PRINT_ERR("filename(%s) too long.\n", filename); + return -1; + } ret = strncat_s(fileFullPath, PATH_MAX, filename, strlen(filename) + 1); if (ret != 0) { PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n", @@ -143,11 +147,10 @@ int32_t PCIDEncode(char *inputFile, char *outDirPath) int32_t ret; char productName[NAME_MAX] = {0}; char *contextBuffer = NULL; - uint32_t bufferLen; + uint32_t bufferLen, osCapSize, privateCapSize; char *convertedBuffer = NULL; uint32_t convertedBufLen = sizeof(PCIDHead); char *systemType = NULL; - int32_t osCapSize, privateCapSize; PCIDHead *headPtr = NULL; char *fillTmpPtr = NULL; cJSON *cjsonObjectRoot = NULL; @@ -206,6 +209,11 @@ int32_t PCIDEncode(char *inputFile, char *outDirPath) } convertedBuffer = (char *)malloc(convertedBufLen); + if (convertedBuffer == NULL) { + PRINT_ERR("malloc failed.\n"); + ret = -1; + goto FREE_CONTEXT_OUT; + } (void)memset_s(convertedBuffer, convertedBufLen, 0, convertedBufLen); @@ -250,7 +258,7 @@ int32_t PCIDEncode(char *inputFile, char *outDirPath) // fill osCap Length *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(osCapSize * SINGLE_FEAT_LENGTH)); fillTmpPtr += sizeof(uint16_t); - for (int32_t i = 0; i < osCapSize; i++) { + for (uint32_t i = 0; i < osCapSize; i++) { arrayItemPtr = cJSON_GetArrayItem(osCapPtr, i); char *pointPos = strchr(arrayItemPtr->valuestring, '.'); if (pointPos == NULL) { @@ -280,7 +288,7 @@ int32_t PCIDEncode(char *inputFile, char *outDirPath) // fill privateCap Length *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(privateCapSize * SINGLE_FEAT_LENGTH)); fillTmpPtr += sizeof(uint16_t); - for (int32_t i = 0; i < privateCapSize; i++) { + for (uint32_t i = 0; i < privateCapSize; i++) { arrayItemPtr = cJSON_GetArrayItem(privateCapPtr, i); char *pointPos = strchr(arrayItemPtr->valuestring, '.'); if (pointPos == NULL) { @@ -425,7 +433,7 @@ int32_t PCIDDecode(char *inputFile, char *outDirPath) ret = -1; goto FREE_SYSCAP_OUT; } - for (int32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) { + for (uint32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) { if (*(osCapArrayPtr + (i + 1) * SINGLE_FEAT_LENGTH - 1) != '\0') { PRINT_ERR("prase file failed, format is invalid, input file : %s\n", inputFile); ret = -1; @@ -569,10 +577,9 @@ int32_t RPCIDEncode(char *inputFile, char *outDirPath) { int32_t ret; char *contextBuffer = NULL; - uint32_t bufferLen; + uint32_t bufferLen, sysCapSize; char *convertedBuffer = NULL; uint32_t convertedBufLen = sizeof(RPCIDHead); - int32_t sysCapSize; RPCIDHead *headPtr = NULL; char *fillTmpPtr = NULL; cJSON *cjsonObjectRoot = NULL; @@ -595,7 +602,7 @@ int32_t RPCIDEncode(char *inputFile, char *outDirPath) sysCapPtr = cJSON_GetObjectItem(cjsonObjectRoot, "syscap"); if (sysCapPtr == NULL || !cJSON_IsArray(sysCapPtr)) { - PRINT_ERR("get \"syscap\" object failed, sysCapPtr = %p\n", sysCapPtr); + PRINT_ERR("get \"syscap\" object failed.\n"); ret = -1; goto FREE_CONTEXT_OUT; } @@ -629,7 +636,7 @@ int32_t RPCIDEncode(char *inputFile, char *outDirPath) // fill osCap Length *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(sysCapSize * SINGLE_FEAT_LENGTH)); fillTmpPtr += sizeof(uint16_t); - for (int32_t i = 0; i < sysCapSize; i++) { + for (uint32_t i = 0; i < sysCapSize; i++) { arrayItemPtr = cJSON_GetArrayItem(sysCapPtr, i); char *pointPos = strchr(arrayItemPtr->valuestring, '.'); if (pointPos == NULL) { @@ -668,7 +675,7 @@ FREE_CONTEXT_OUT: int32_t RPCIDDecode(char *inputFile, char *outDirPath) { - uint32_t ret; + int32_t ret; char *contextBuffer = NULL; char *contextBufferTail = NULL; uint32_t bufferLen; diff --git a/test/unittest/common/syscap_codec_test.cpp b/test/unittest/common/syscap_codec_test.cpp index c8f39e1..a5a30ac 100644 --- a/test/unittest/common/syscap_codec_test.cpp +++ b/test/unittest/common/syscap_codec_test.cpp @@ -34,8 +34,8 @@ void SyscapCodecTest::TearDown() {} */ HWTEST_F(SyscapCodecTest, EncodeOsSyscap, TestSize.Level1) { - char OsInput[128] = {0}; - EXPECT_TRUE(EncodeOsSyscap(OsInput)); + char OsInput[MAX_SYSCAP_STR_LEN] = {0}; + EXPECT_TRUE(EncodeOsSyscap(OsInput, MAX_SYSCAP_STR_LEN)); } /* @@ -59,12 +59,12 @@ HWTEST_F(SyscapCodecTest, EncodePrivateSyscap, TestSize.Level1) HWTEST_F(SyscapCodecTest, DecodeOsSyscap, TestSize.Level1) { int osSyscap[32] = {1, 3, 3}; - char (*osOutput)[128] = NULL; + char (*osOutput)[MAX_SYSCAP_STR_LEN] = NULL; int decodeOsCnt; char expectOsOutput001[] = "SystemCapability.Account.AppAccount"; char expectOsOutput002[] = "SystemCapability.Account.OsAccount"; EXPECT_TRUE(DecodeOsSyscap((char *)osSyscap, &osOutput, &decodeOsCnt)); - char (*tmpOsOutput)[128] = osOutput; + char (*tmpOsOutput)[MAX_SYSCAP_STR_LEN] = osOutput; EXPECT_STREQ(*tmpOsOutput, expectOsOutput001); EXPECT_STREQ(*(tmpOsOutput + 1), expectOsOutput002); EXPECT_EQ(decodeOsCnt, 2); @@ -78,7 +78,7 @@ HWTEST_F(SyscapCodecTest, DecodeOsSyscap, TestSize.Level1) */ HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1) { - char (*priOutput)[128] = NULL; + char (*priOutput)[MAX_SYSCAP_STR_LEN] = NULL; char priSyscap[] = "Device.syscap1GEDR,Device.syscap2WREGW,Vendor.syscap3RGD,Vendor.syscap4RWEG,Vendor.syscap5REWGWE,"; int decodePriCnt; char expectPriOutput001[] = "SystemCapability.Device.syscap1GEDR"; @@ -87,7 +87,7 @@ HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1) char expectPriOutput004[] = "SystemCapability.Vendor.syscap4RWEG"; char expectPriOutput005[] = "SystemCapability.Vendor.syscap5REWGWE"; EXPECT_TRUE(DecodePrivateSyscap(priSyscap, &priOutput, &decodePriCnt)); - char (*tmpPtiOutput)[128] = priOutput; + char (*tmpPtiOutput)[MAX_SYSCAP_STR_LEN] = priOutput; EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput001); EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput002); EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput003); -- Gitee