From f4d79466a083fe238c9f04b4bb7e7d930fff395d Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Fri, 19 Jul 2024 16:16:07 +0200 Subject: librc-misc.c, rc.c, rc-status.c: fix memory leaks Signed-off-by: Anna (navi) Figueiredo Gomes --- src/librc/librc-misc.c | 59 +++++++++++++++++++++++------------------------ src/openrc/rc.c | 6 ++++- src/rc-status/rc-status.c | 2 ++ 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/librc/librc-misc.c b/src/librc/librc-misc.c index 538a67c2..2fbb2d8a 100644 --- a/src/librc/librc-misc.c +++ b/src/librc/librc-misc.c @@ -131,8 +131,9 @@ rc_proc_getent(const char *ent RC_UNUSED) { #ifdef __linux__ FILE *fp; - char *proc, *p, *value = NULL; + char *proc = NULL, *p, *value = NULL, *save; size_t i, len; + ssize_t size; if (!exists("/proc/cmdline")) return NULL; @@ -140,23 +141,21 @@ rc_proc_getent(const char *ent RC_UNUSED) if (!(fp = fopen("/proc/cmdline", "r"))) return NULL; - proc = NULL; i = 0; - if (rc_getline(&proc, &i, fp) == -1 || proc == NULL) + if ((size = getline(&proc, &i, fp) == -1)) { + free(proc); return NULL; - - if (proc != NULL) { - len = strlen(ent); - - while ((p = strsep(&proc, " "))) { - if (strncmp(ent, p, len) == 0 && (p[len] == '\0' || p[len] == ' ' || p[len] == '=')) { - p += len; - - if (*p == '=') - p++; - - value = xstrdup(p); - } + } + proc[size - 1] = '\0'; + save = proc; + + len = strlen(ent); + while ((p = strsep(&save, " "))) { + if (strncmp(ent, p, len) == 0 && (p[len] == '\0' || p[len] == ' ' || p[len] == '=')) { + p += len; + if (*p == '=') + p++; + value = xstrdup(p); } } @@ -332,13 +331,14 @@ static RC_STRINGLIST * rc_config_directory(RC_STRINGLIST *config) { DIR *dp; struct dirent *d; - RC_STRINGLIST *rc_conf_d_files = rc_stringlist_new(); + RC_STRINGLIST *rc_conf_d_files; RC_STRING *fname; RC_STRINGLIST *rc_conf_d_list; char path[PATH_MAX]; RC_STRING *line; if ((dp = opendir(RC_CONF_D)) != NULL) { + rc_conf_d_files = rc_stringlist_new(); while ((d = readdir(dp)) != NULL) { if (fnmatch("*.conf", d->d_name, FNM_PATHNAME) == 0) { rc_stringlist_addu(rc_conf_d_files, d->d_name); @@ -346,22 +346,21 @@ static RC_STRINGLIST * rc_config_directory(RC_STRINGLIST *config) } closedir(dp); - if (rc_conf_d_files) { - rc_stringlist_sort(&rc_conf_d_files); - TAILQ_FOREACH(fname, rc_conf_d_files, entries) { - if (!fname->value) - continue; - sprintf(path, "%s/%s", RC_CONF_D, fname->value); - rc_conf_d_list = rc_config_list(path); - TAILQ_FOREACH(line, rc_conf_d_list, entries) - if (line->value) - rc_config_set_value(config, line->value); - rc_stringlist_free(rc_conf_d_list); - } + rc_stringlist_sort(&rc_conf_d_files); + TAILQ_FOREACH(fname, rc_conf_d_files, entries) { + if (!fname->value) + continue; + sprintf(path, "%s/%s", RC_CONF_D, fname->value); + rc_conf_d_list = rc_config_list(path); + TAILQ_FOREACH(line, rc_conf_d_list, entries) + if (line->value) + rc_config_set_value(config, line->value); + rc_stringlist_free(rc_conf_d_list); } + + rc_stringlist_free(rc_conf_d_files); } - rc_stringlist_free(rc_conf_d_files); return config; } diff --git a/src/openrc/rc.c b/src/openrc/rc.c index 46e3022d..48279a84 100644 --- a/src/openrc/rc.c +++ b/src/openrc/rc.c @@ -557,12 +557,13 @@ do_stop_services(RC_STRINGLIST *types_nw, RC_STRINGLIST *start_services, pid_t pid; RC_STRING *service, *svc1, *svc2; RC_STRINGLIST *deporder, *tmplist, *kwords; + RC_STRINGLIST *types_nw_save = NULL; RC_SERVICE state; RC_STRINGLIST *nostop; bool crashed, nstop; if (!types_nw) { - types_nw = rc_stringlist_new(); + types_nw = types_nw_save = rc_stringlist_new(); rc_stringlist_add(types_nw, "needsme"); rc_stringlist_add(types_nw, "wantsme"); } @@ -652,6 +653,9 @@ stop: } } + if (types_nw_save) + rc_stringlist_free(types_nw_save); + rc_stringlist_free(nostop); } diff --git a/src/rc-status/rc-status.c b/src/rc-status/rc-status.c index 78d5a6af..2a6cfda6 100644 --- a/src/rc-status/rc-status.c +++ b/src/rc-status/rc-status.c @@ -131,6 +131,8 @@ static char *get_uptime(const char *service) "%02"PRId64":%02"PRId64":%02"PRId64" (%s)", diff_hours, diff_mins, diff_secs, start_count); } + free(start_count); + free(start_time_string); } return uptime; } -- cgit v1.2.3