diff --git a/frameworks/config_policy/src/config_policy_utils.c b/frameworks/config_policy/src/config_policy_utils.c index 9dcf4d7f644cc70e91391f94acb15a8cf77e1316..e02c711f9cc75192db2f53fc3909af12af36074f 100644 --- a/frameworks/config_policy/src/config_policy_utils.c +++ b/frameworks/config_policy/src/config_policy_utils.c @@ -474,6 +474,24 @@ char *GetOneCfgFile(const char *pathSuffix, char *buf, unsigned int bufLength) return GetOneCfgFileEx(pathSuffix, buf, bufLength, FOLLOWX_MODE_DEFAULT, NULL); } +char *StrDupCustom(const char *buf) +{ + if (buf == NULL) { + return NULL; + } + size_t len = strlen(buf); + char *paths = (char*)malloc(sizeof(char) * (len + 1)); + if (paths == NULL) { + return NULL; + } + errno_t ret = strcpy_s(paths, len + 1, buf); + if (ret != 0) { + free(paths); + paths = NULL; + } + return paths; +} + CfgFiles *GetCfgFilesEx(const char *pathSuffix, int followMode, const char *extra) { if (pathSuffix == NULL) { @@ -499,14 +517,21 @@ CfgFiles *GetCfgFilesEx(const char *pathSuffix, int followMode, const char *extr } if (snprintf_s(buf, MAX_PATH_LEN, MAX_PATH_LEN - 1, "%s/%s", dirs->paths[i], pathSuffix) > 0 && access(buf, F_OK) == 0) { - files->paths[index++] = strdup(buf); + files->paths[index] = StrDupCustom(buf); + if (files->paths[index] != NULL) { + index++; + } } for (int j = 0; result && j < result->segCount && index < MAX_CFG_POLICY_DIRS_CNT; j++) { - if (result->segs[j] && - snprintf_s(buf, MAX_PATH_LEN, MAX_PATH_LEN - 1, "%s/%s/%s", dirs->paths[i], result->segs[j], - pathSuffix) > 0 && - access(buf, F_OK) == 0) { - files->paths[index++] = strdup(buf); + if ((!result->segs[j]) || + (snprintf_s(buf, MAX_PATH_LEN, MAX_PATH_LEN - 1, "%s/%s/%s", dirs->paths[i], result->segs[j], + pathSuffix) <= 0) || + (access(buf, F_OK) != 0)) { + continue; + } + files->paths[index] = StrDupCustom(buf); + if (files->paths[index] != NULL) { + index++; } } }