X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=9a53330004931408df017fb4b2ebbd87ad31a61e;hp=4e78767dfb46d2c18b47670174a21bbbeafbdfb3;hb=d7ba13889c8692b14f99238ddf2721ed78df89d2;hpb=6c1c0768320135c6936c371b09731851b508c023 diff --git a/src/common/utils.c b/src/common/utils.c index 4e78767df..9a5333000 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -35,6 +35,7 @@ #include #include +#include #include "utils.h" #include "defaults.h" @@ -91,6 +92,10 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) /* Cut the part we will be trying to resolve */ cut_path = strndup(path, next - path); + if (cut_path == NULL) { + PERROR("strndup"); + goto error; + } /* Try to resolve this part */ try_path = realpath((char *)cut_path, NULL); @@ -144,6 +149,10 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) * path are pointers for the same memory space */ cut_path = strdup(prev); + if (cut_path == NULL) { + PERROR("strdup"); + goto error; + } /* Concatenate the strings */ snprintf(resolved_path, size, "%s%s", try_path_prev, cut_path); @@ -217,7 +226,10 @@ char *utils_expand_path(const char *path) /* We prepare the start_path not containing it */ start_path = strndup(absolute_path, next - absolute_path); - + if (!start_path) { + PERROR("strndup"); + goto error; + } /* And we concatenate it with the part after this string */ snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 2); @@ -234,6 +246,10 @@ char *utils_expand_path(const char *path) /* Then we prepare the start_path not containing it */ start_path = strndup(absolute_path, previous - absolute_path); + if (!start_path) { + PERROR("strndup"); + goto error; + } /* And we concatenate it with the part after the '/../' */ snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 4); @@ -462,10 +478,14 @@ int utils_create_pid_file(pid_t pid, const char *filepath) ret = fprintf(fp, "%d\n", pid); if (ret < 0) { PERROR("fprintf pid file"); + goto error; } - fclose(fp); + if (fclose(fp)) { + PERROR("fclose"); + } DBG("Pid %d written in file %s", pid, filepath); + ret = 0; error: return ret; } @@ -497,9 +517,11 @@ int utils_create_lock_file(const char *filepath) */ ret = flock(fd, LOCK_EX | LOCK_NB); if (ret) { - WARN("Could not get lock file %s, another instance is running.", + ERR("Could not get lock file %s, another instance is running.", filepath); - close(fd); + if (close(fd)) { + PERROR("close lock file"); + } fd = ret; goto error; } @@ -864,11 +886,11 @@ char *utils_get_home_dir(void) char *val = NULL; struct passwd *pwd; - val = getenv(DEFAULT_LTTNG_HOME_ENV_VAR); + val = lttng_secure_getenv(DEFAULT_LTTNG_HOME_ENV_VAR); if (val != NULL) { goto end; } - val = getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); + val = lttng_secure_getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); if (val != NULL) { goto end; } @@ -933,7 +955,7 @@ end: LTTNG_HIDDEN char *utils_get_kmod_probes_list(void) { - return getenv(DEFAULT_LTTNG_KMOD_PROBES); + return lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES); } /* @@ -943,7 +965,7 @@ char *utils_get_kmod_probes_list(void) LTTNG_HIDDEN char *utils_get_extra_kmod_probes_list(void) { - return getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES); + return lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES); } /*