diff --git a/HISTORY b/HISTORY index 86e59d5..0d875ae 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ Version 1.56 2022-01-29 * add function fc_gettid + * function normalize_path: NULL from parameter for getcwd Version 1.55 2022-01-12 * fastcommon php extension adapt to php 8 diff --git a/src/shared_func.c b/src/shared_func.c index 8680f38..4c6b641 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -3034,13 +3034,11 @@ int normalize_path(const char *from, const char *filename, const char *start; const char *last; const char *end; + char cwd[PATH_MAX]; int up_count; int path_len; int i; - if (IS_FILE_RESOURCE(from)) { - from = from + FILE_RESOURCE_TAG_LEN; - } if (IS_FILE_RESOURCE(filename)) { filename = filename + FILE_RESOURCE_TAG_LEN; } @@ -3049,6 +3047,33 @@ int normalize_path(const char *from, const char *filename, return snprintf(full_filename, size, "%s", filename); } + if (from == NULL) { + if (getcwd(cwd, sizeof(cwd)) == NULL) { + logError("file: "__FILE__", line: %d, " + "call getcwd fail, errno: %d, error info: %s", + __LINE__, errno, STRERROR(errno)); + *full_filename = '\0'; + return 0; + } + + path_len = strlen(cwd); + if (cwd[path_len - 1] != '/') { + if ((path_len + 1) >= sizeof(cwd)) { + logError("file: "__FILE__", line: %d, " + "cwd length is too long, exceeds %d", + __LINE__, (int)sizeof(cwd)); + *full_filename = '\0'; + return 0; + } + + cwd[path_len] = '/'; + cwd[path_len + 1] = '\0'; + } + from = cwd; + } else if (IS_FILE_RESOURCE(from)) { + from = from + FILE_RESOURCE_TAG_LEN; + } + last = strrchr(from, '/'); if (last != NULL) { end = filename + strlen(filename); diff --git a/src/shared_func.h b/src/shared_func.h index 272ade3..6d9e3fa 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -979,7 +979,8 @@ char *format_http_date(time_t t, BufferInfo *buffer); /** return full path for the filename (the second parameter) * parameters: - * from: the input full path filename to get base path + * from: the input full path filename to get base path, + * NULL for current work directory * filename: the filename to resolve path * full_filename: store the resolved full path filename * size: the max size of full_filename