From 4f00620d66e81d89a546b29a774dad49d38983b3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Sat, 24 Aug 2019 15:14:11 -0700 Subject: [PATCH] Cleanup: mark utils_get_home_dir as returning a const string MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit utils_get_home_dir() returns a string obtained from getenv() that should not be modified nor free'd. However, utils_get_user_home_dir() returns the path as a copy. The return parameter of utils_get_home_dir() is marked as const to underline this difference and prevent mix-ups in the use of those functions. Signed-off-by: Jérémie Galarneau --- src/bin/lttng-relayd/health-relayd.c | 3 ++- src/bin/lttng-relayd/session.c | 2 +- src/bin/lttng-relayd/utils.c | 2 +- src/bin/lttng-sessiond/notification-thread.c | 2 +- src/bin/lttng/conf.c | 6 +++--- src/bin/lttng/utils.c | 3 ++- src/common/config/session-config.c | 4 ++-- src/common/utils.c | 2 +- src/common/utils.h | 2 +- 9 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/bin/lttng-relayd/health-relayd.c b/src/bin/lttng-relayd/health-relayd.c index 4a2dc65a6..482be8eea 100644 --- a/src/bin/lttng-relayd/health-relayd.c +++ b/src/bin/lttng-relayd/health-relayd.c @@ -155,7 +155,8 @@ static int setup_health_path(void) { int is_root, ret = 0; - char *home_path = NULL, *rundir = NULL, *relayd_path = NULL; + const char *home_path = NULL; + char *rundir = NULL, *relayd_path = NULL; ret = parse_health_env(); if (ret) { diff --git a/src/bin/lttng-relayd/session.c b/src/bin/lttng-relayd/session.c index 1359e9f4b..5eedffc94 100644 --- a/src/bin/lttng-relayd/session.c +++ b/src/bin/lttng-relayd/session.c @@ -39,7 +39,7 @@ static int session_set_anonymous_chunk(struct relay_session *session) struct lttng_trace_chunk *chunk = NULL; enum lttng_trace_chunk_status status; struct lttng_directory_handle output_directory; - char *base_path = opt_output_path; + const char *base_path = opt_output_path; if (base_path == NULL) { /* No output path defined */ diff --git a/src/bin/lttng-relayd/utils.c b/src/bin/lttng-relayd/utils.c index 7ae0879e5..db124bc35 100644 --- a/src/bin/lttng-relayd/utils.c +++ b/src/bin/lttng-relayd/utils.c @@ -33,7 +33,7 @@ static char *create_output_path_auto(const char *path_name) { int ret; char *traces_path = NULL; - char *default_path; + const char *default_path; default_path = utils_get_home_dir(); if (default_path == NULL) { diff --git a/src/bin/lttng-sessiond/notification-thread.c b/src/bin/lttng-sessiond/notification-thread.c index 8c2892bda..3686d72f7 100644 --- a/src/bin/lttng-sessiond/notification-thread.c +++ b/src/bin/lttng-sessiond/notification-thread.c @@ -172,7 +172,7 @@ char *get_notification_channel_sock_path(void) goto error; } } else { - char *home_path = utils_get_home_dir(); + const char *home_path = utils_get_home_dir(); if (!home_path) { ERR("Can't get HOME directory for socket creation"); diff --git a/src/bin/lttng/conf.c b/src/bin/lttng/conf.c index 52979af0d..6de527553 100644 --- a/src/bin/lttng/conf.c +++ b/src/bin/lttng/conf.c @@ -77,7 +77,7 @@ error: * On success, returns 0; * on error, returns -1. */ -static int create_config_file(char *path) +static int create_config_file(const char *path) { int ret; FILE *fp; @@ -155,7 +155,7 @@ end: */ void config_destroy_default(void) { - char *path = utils_get_home_dir(); + const char *path = utils_get_home_dir(); if (path == NULL) { return; } @@ -302,7 +302,7 @@ error: int config_init(const char *session_name) { int ret; - char *path; + const char *path; path = utils_get_home_dir(); if (path == NULL) { diff --git a/src/bin/lttng/utils.c b/src/bin/lttng/utils.c index 1e3b91344..fa2d3c9da 100644 --- a/src/bin/lttng/utils.c +++ b/src/bin/lttng/utils.c @@ -51,7 +51,8 @@ static const char *str_function = "Function"; static char *_get_session_name(int quiet) { - char *path, *session_name = NULL; + const char *path; + char *session_name = NULL; /* Get path to config file */ path = utils_get_home_dir(); diff --git a/src/common/config/session-config.c b/src/common/config/session-config.c index 8e5410c3f..40019514b 100644 --- a/src/common/config/session-config.c +++ b/src/common/config/session-config.c @@ -250,7 +250,7 @@ int config_get_section_entries(const char *override_path, const char *section, config_entry_handler_cb handler, void *user_data) { int ret = 0; - char *path; + const char *path; FILE *config_file = NULL; struct handler_filter_args filter = { section, handler, user_data }; @@ -3532,7 +3532,7 @@ int config_load_session(const char *path, const char *session_name, } if (!path) { - char *home_path; + const char *home_path; const char *sys_path; /* Try home path */ diff --git a/src/common/utils.c b/src/common/utils.c index 664ba3e8e..fda43152d 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -1197,7 +1197,7 @@ int utils_get_count_order_u64(uint64_t x) * Otherwise returns the value of HOME. */ LTTNG_HIDDEN -char *utils_get_home_dir(void) +const char *utils_get_home_dir(void) { char *val = NULL; struct passwd *pwd; diff --git a/src/common/utils.h b/src/common/utils.h index 57c7307be..00b65939d 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -55,7 +55,7 @@ int utils_parse_size_suffix(char const * const str, uint64_t * const size); int utils_parse_time_suffix(char const * const str, uint64_t * const time_us); int utils_get_count_order_u32(uint32_t x); int utils_get_count_order_u64(uint64_t x); -char *utils_get_home_dir(void); +const char *utils_get_home_dir(void); char *utils_get_user_home_dir(uid_t uid); size_t utils_get_current_time_str(const char *format, char *dst, size_t len); int utils_get_group_id(const char *name, bool warn, gid_t *gid); -- 2.34.1