X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=3dc2d9297d4379ee99a6e127b17a84748dbf15de;hp=84d42f4638e53f37fc139c4b1c6d5d6b1c30b375;hb=3f322f1a1a1ce2fc1354699f11ac483d7a3cd5fc;hpb=4b223a6755662f272f7db155ee380528728e5dd1 diff --git a/src/common/utils.c b/src/common/utils.c index 84d42f463..3dc2d9297 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -41,6 +41,7 @@ #include "utils.h" #include "defaults.h" +#include "time.h" /* * Return a partial realpath(3) of the path even if the full path does not @@ -197,7 +198,7 @@ error: } static -char *expand_double_slashes_dot_and_dotdot(char *path) +int expand_double_slashes_dot_and_dotdot(char *path) { size_t expanded_path_len, path_len; const char *curr_char, *path_last_char, *next_slash, *prev_slash; @@ -206,7 +207,6 @@ char *expand_double_slashes_dot_and_dotdot(char *path) path_last_char = &path[path_len]; if (path_len == 0) { - path = NULL; goto error; } @@ -292,9 +292,9 @@ char *expand_double_slashes_dot_and_dotdot(char *path) } path[expanded_path_len] = '\0'; - + return 0; error: - return path; + return -1; } /* @@ -310,9 +310,10 @@ error: LTTNG_HIDDEN char *_utils_expand_path(const char *path, bool keep_symlink) { + int ret; char *absolute_path = NULL; char *last_token; - int is_dot, is_dotdot; + bool is_dot, is_dotdot; /* Safety net */ if (path == NULL) { @@ -320,41 +321,51 @@ char *_utils_expand_path(const char *path, bool keep_symlink) } /* Allocate memory for the absolute_path */ - absolute_path = zmalloc(PATH_MAX); + absolute_path = zmalloc(LTTNG_PATH_MAX); if (absolute_path == NULL) { PERROR("zmalloc expand path"); goto error; } if (path[0] == '/') { - strncpy(absolute_path, path, PATH_MAX); + ret = lttng_strncpy(absolute_path, path, LTTNG_PATH_MAX); + if (ret) { + ERR("Path exceeds maximal size of %i bytes", LTTNG_PATH_MAX); + goto error; + } } else { /* * This is a relative path. We need to get the present working * directory and start the path walk from there. */ - char current_working_dir[PATH_MAX]; + char current_working_dir[LTTNG_PATH_MAX]; char *cwd_ret; + cwd_ret = getcwd(current_working_dir, sizeof(current_working_dir)); if (!cwd_ret) { - absolute_path = NULL; goto error; } /* * Get the number of character in the CWD and allocate an array * to can hold it and the path provided by the caller. */ - snprintf(absolute_path, PATH_MAX, "%s/%s", current_working_dir, path); + ret = snprintf(absolute_path, LTTNG_PATH_MAX, "%s/%s", + current_working_dir, path); + if (ret >= LTTNG_PATH_MAX) { + ERR("Concatenating current working directory %s and path %s exceeds maximal size of %i bytes", + current_working_dir, path, LTTNG_PATH_MAX); + goto error; + } } if (keep_symlink) { /* Resolve partially our path */ absolute_path = utils_partial_realpath(absolute_path, - absolute_path, PATH_MAX); + absolute_path, LTTNG_PATH_MAX); } - absolute_path = expand_double_slashes_dot_and_dotdot(absolute_path); - if (!absolute_path) { + ret = expand_double_slashes_dot_and_dotdot(absolute_path); + if (ret) { goto error; }