X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=3442bef8922a276120e70cab43d3b46ca97ec05f;hb=f726677b9b3a5a384bb96ea4ef7d6f0c4b03524f;hp=78bfbef95ede423f4a6c028fbfb0a3886e3c0ea8;hpb=03e301513e0f0bb552ed5fd1cc8056900396652d;p=lttng-tools.git diff --git a/src/common/utils.c b/src/common/utils.c index 78bfbef95..3442bef89 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -197,7 +197,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 +206,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 +291,9 @@ char *expand_double_slashes_dot_and_dotdot(char *path) } path[expanded_path_len] = '\0'; - + return 0; error: - return path; + return -1; } /* @@ -310,9 +309,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,21 +320,26 @@ 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) { goto error; @@ -343,17 +348,23 @@ char *_utils_expand_path(const char *path, bool keep_symlink) * 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; }