diff --git a/BUILD.gn b/BUILD.gn index f38e04bbecd5704d82baf33786eb60b08e6ced84..630ec5883ce355e987d43b5e3048481d02f3a709 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -34,7 +34,6 @@ ohos_executable("syscap_tool_bin") { if (is_mingw) { defines += [ "_POSIX_" ] } - sources = [ "./src/main.c" ] sources += sources_platform_common diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index e3ffd83e1950d80748f83180c80cdb6df09e7ff2..d96f95e91e6791e2b02c02ff2a47b4231e3c441c 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -36,8 +36,6 @@ printf(__VA_ARGS__); \ } while (0) -static char *inputFile = "/system/etc/PCID.sc"; - static void FreeContextBuffer(char *contextBuffer) { (void)free(contextBuffer); @@ -49,6 +47,7 @@ static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) FILE *fp = NULL; struct stat statBuf; char *contextBuffer = NULL; + const char *inputFile = "/system/etc/PCID.sc"; ret = stat(inputFile, &statBuf); if (ret != 0) { @@ -86,7 +85,7 @@ static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) return 0; } -bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN], int length) +bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN]) { int32_t ret; int32_t res; diff --git a/interfaces/inner_api/syscap_interface.h b/interfaces/inner_api/syscap_interface.h index 6535a1fb22d5f185c6c4195cc0331d21720fe894..a225479690451556cf531a189cb8a9d32f33bbe8 100644 --- a/interfaces/inner_api/syscap_interface.h +++ b/interfaces/inner_api/syscap_interface.h @@ -27,7 +27,7 @@ extern "C" { #define MAX_SYSCAP_STR_LEN 128 -bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN], int length); +bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN]); 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 4c012e6f9e984a60bd5fe63aa24c6c6bc14aa5e9..2ff3fb1a3fa844947fe3bce514238e83f2ca06d2 100644 --- a/napi/napi_query_syscap.cpp +++ b/napi/napi_query_syscap.cpp @@ -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, SYSCAP_STR_MAX_LEN); + retBool = EncodeOsSyscap(osOutput); if (!retBool) { PRINT_ERR("get encoded os syscap failed."); return nullptr; diff --git a/src/create_pcid.c b/src/create_pcid.c index 2e511c94e81107bfe21d02cd7d93f7b35a14604e..efd478cae0247c6c965bd10c1b9518f0a2b9fcc8 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -53,14 +53,27 @@ static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *c FILE *fp = NULL; struct stat statBuf; char *contextBuffer = NULL; + char path[PATH_MAX + 1] = {0x00}; - ret = stat(inputFile, &statBuf); +#ifdef _POSIX_ + if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) { + PRINT_ERR("get path(%s) failed\n", inputFile); + return -1; + } +#else + if (strlen(inputFile) > PATH_MAX || realpath(inputFile, path) == NULL) { + PRINT_ERR("get file(%s) real path failed\n", inputFile); + return -1; + } +#endif + + ret = stat(path, &statBuf); if (ret != 0) { - PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", inputFile, errno); + PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno); return -1; } if (!(statBuf.st_mode & S_IRUSR)) { - PRINT_ERR("don't have permission to read the file(%s)\n", inputFile); + PRINT_ERR("don't have permission to read the file(%s)\n", path); return -1; } contextBuffer = (char *)malloc(statBuf.st_size + 1); @@ -69,15 +82,15 @@ static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *c return -1; } - fp = fopen(inputFile, "rb"); + fp = fopen(path, "rb"); if (fp == NULL) { - PRINT_ERR("open file(%s) failed, errno = %d\n", inputFile, errno); + PRINT_ERR("open file(%s) failed, errno = %d\n", path, errno); FreeContextBuffer(contextBuffer); return -1; } ret = fread(contextBuffer, statBuf.st_size, 1, fp); if (ret != 1) { - PRINT_ERR("read file(%s) failed, errno = %d\n", inputFile, errno); + PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno); FreeContextBuffer(contextBuffer); (void)fclose(fp); return -1; @@ -95,38 +108,44 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename { int32_t ret; FILE *fp = NULL; - char fileFullPath[PATH_MAX] = {0}; int32_t pathLen = strlen(outDirPath); + char path[PATH_MAX + 1] = {0x00}; - ret = strncpy_s(fileFullPath, PATH_MAX, outDirPath, pathLen + 1); - if (ret != 0) { - PRINT_ERR("strncpy_s failed, source string:%s, len = %d, errno = %d\n", outDirPath, pathLen + 1, errno); +#ifdef _POSIX_ + 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) { + PRINT_ERR("get file(%s) real path failed\n", outDirPath); return -1; } +#endif - if (fileFullPath[pathLen - 1] != '/' && fileFullPath[pathLen - 1] != '\\') { - fileFullPath[pathLen] = '/'; + if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') { + path[pathLen] = '/'; } - if (strlen(fileFullPath) + strlen(filename) + 1 > PATH_MAX) { + if (strlen(path) + 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); + ret = strncat_s(path, PATH_MAX, filename, strlen(filename) + 1); if (ret != 0) { PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n", - fileFullPath, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno); + path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno); return -1; } - fp = fopen(fileFullPath, "wb"); + fp = fopen(path, "wb"); if (fp == NULL) { - PRINT_ERR("can't create file(%s), errno = %d\n", fileFullPath, errno); + PRINT_ERR("can't create file(%s), errno = %d\n", path, errno); return -1; } if (fwrite(convertedBuffer, contextBufLen, 1, fp) != 1) { - PRINT_ERR("can't write file(%s),errno = %d\n", fileFullPath, errno); + PRINT_ERR("can't write file(%s),errno = %d\n", path, errno); (void)fclose(fp); return -1; } @@ -148,8 +167,8 @@ static cJSON *CreateWholeSyscapJsonObj(void) int32_t CreatePCID(char *inputFile, char *outDirPath) { - int32_t ret, osCapSize, sectorOfBits, posOfBits; - uint32_t i, contextBufLen, privateCapSize; + int32_t ret, sectorOfBits, posOfBits; + uint32_t i, contextBufLen, privateCapSize, osCapSize; errno_t nRet = 0; char *contextBuffer = NULL; char *systemType = NULL; diff --git a/src/syscap_tool.c b/src/syscap_tool.c index bf681a0cdcc151118701dc571321a4e5c88aa19f..88463681279edbbd80e0ac1a93d2e591a5f15225 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -60,14 +60,27 @@ static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *b FILE *fp = NULL; struct stat statBuf; char *contextBuffer = NULL; + char path[PATH_MAX + 1] = {0x00}; - ret = stat(inputFile, &statBuf); +#ifdef _POSIX_ + if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) { + PRINT_ERR("get path(%s) failed\n", inputFile); + return -1; + } +#else + if (strlen(inputFile) > PATH_MAX || realpath(inputFile, path) == NULL) { + PRINT_ERR("get file(%s) real path failed\n", inputFile); + return -1; + } +#endif + + ret = stat(path, &statBuf); if (ret != 0) { - PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", inputFile, errno); + PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno); return -1; } if (!(statBuf.st_mode & S_IRUSR)) { - PRINT_ERR("don't have permission to read the file(%s)\n", inputFile); + PRINT_ERR("don't have permission to read the file(%s)\n", path); return -1; } contextBuffer = (char *)malloc(statBuf.st_size + 1); @@ -75,15 +88,15 @@ static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *b PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); return -1; } - fp = fopen(inputFile, "rb"); + fp = fopen(path, "rb"); if (fp == NULL) { - PRINT_ERR("open file(%s) failed, errno = %d\n", inputFile, errno); + PRINT_ERR("open file(%s) failed, errno = %d\n", path, errno); FreeContextBuffer(contextBuffer); return -1; } ret = fread(contextBuffer, statBuf.st_size, 1, fp); if (ret != 1) { - PRINT_ERR("read file(%s) failed, errno = %d\n", inputFile, errno); + PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno); FreeContextBuffer(contextBuffer); (void)fclose(fp); return -1; @@ -100,39 +113,45 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, char *filename, char { int32_t ret; FILE *fp = NULL; - char fileFullPath[PATH_MAX] = {0}; + char path[PATH_MAX + 1] = {0x00}; int32_t pathLen = strlen(outDirPath); - ret = strncpy_s(fileFullPath, PATH_MAX, outDirPath, pathLen + 1); - if (ret != 0) { - PRINT_ERR("strncpy_s failed, source string:%s, len = %d, errno = %d\n", outDirPath, pathLen + 1, errno); +#ifdef _POSIX_ + 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) { + PRINT_ERR("get file(%s) real path failed\n", outDirPath); return -1; } +#endif - if (fileFullPath[pathLen - 1] != '/' && fileFullPath[pathLen - 1] != '\\') { - fileFullPath[pathLen] = '/'; + if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') { + path[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); + ret = strncat_s(path, PATH_MAX, filename, strlen(filename)); if (ret != 0) { PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n", - fileFullPath, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno); + path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno); return -1; } - fp = fopen(fileFullPath, "wb"); + fp = fopen(path, "wb"); if (fp == NULL) { - PRINT_ERR("can`t create file(%s), errno = %d\n", fileFullPath, errno); + PRINT_ERR("can`t create file(%s), errno = %d\n", path, errno); return -1; } size_t retFwrite = fwrite(convertedBuffer, bufferLen, 1, fp); if (retFwrite != 1) { - PRINT_ERR("can`t write file(%s),errno = %d\n", fileFullPath, errno); + PRINT_ERR("can`t write file(%s),errno = %d\n", path, errno); (void)fclose(fp); return -1; } @@ -732,7 +751,7 @@ int32_t RPCIDDecode(char *inputFile, char *outDirPath) ret = -1; goto FREE_CONTEXT_OUT; } - for (int32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) { + for (uint32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) { if (*(sysCapArrayPtr + (i + 1) * SINGLE_FEAT_LENGTH - 1) != '\0') { PRINT_ERR("prase file failed, format is invalid, input file : %s\n", inputFile); ret = -1; diff --git a/test/unittest/common/syscap_codec_test.cpp b/test/unittest/common/syscap_codec_test.cpp index a5a30ac49e74fc5de7e621d2e14e190e69b2dbd2..356e27164a50826887ef108bc7032103fdcb6cd2 100644 --- a/test/unittest/common/syscap_codec_test.cpp +++ b/test/unittest/common/syscap_codec_test.cpp @@ -35,7 +35,7 @@ void SyscapCodecTest::TearDown() {} HWTEST_F(SyscapCodecTest, EncodeOsSyscap, TestSize.Level1) { char OsInput[MAX_SYSCAP_STR_LEN] = {0}; - EXPECT_TRUE(EncodeOsSyscap(OsInput, MAX_SYSCAP_STR_LEN)); + EXPECT_TRUE(EncodeOsSyscap(OsInput)); } /*