1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-05 06:52:22 +03:00

timedated: fix a few memory leaks

Contents of /etc/adjtime and more.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2013-04-02 16:07:21 -04:00
parent 917b5dc707
commit d257f05a5f

View File

@ -216,7 +216,7 @@ static int write_data_timezone(void) {
static int write_data_local_rtc(void) { static int write_data_local_rtc(void) {
int r; int r;
char *s, *w; char _cleanup_free_ *s = NULL, *w = NULL;
r = read_full_file("/etc/adjtime", &s, NULL); r = read_full_file("/etc/adjtime", &s, NULL);
if (r < 0) { if (r < 0) {
@ -234,55 +234,42 @@ static int write_data_local_rtc(void) {
size_t a, b; size_t a, b;
p = strchr(s, '\n'); p = strchr(s, '\n');
if (!p) { if (!p)
free(s);
return -EIO; return -EIO;
}
p = strchr(p+1, '\n'); p = strchr(p+1, '\n');
if (!p) { if (!p)
free(s);
return -EIO; return -EIO;
}
p++; p++;
e = strchr(p, '\n'); e = strchr(p, '\n');
if (!e) { if (!e)
free(s);
return -EIO; return -EIO;
}
a = p - s; a = p - s;
b = strlen(e); b = strlen(e);
w = new(char, a + (tz.local_rtc ? 5 : 3) + b + 1); w = new(char, a + (tz.local_rtc ? 5 : 3) + b + 1);
if (!w) { if (!w)
free(s);
return -ENOMEM; return -ENOMEM;
}
*(char*) mempcpy(stpcpy(mempcpy(w, s, a), tz.local_rtc ? "LOCAL" : "UTC"), e, b) = 0; *(char*) mempcpy(stpcpy(mempcpy(w, s, a), tz.local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
if (streq(w, NULL_ADJTIME_UTC)) { if (streq(w, NULL_ADJTIME_UTC)) {
free(w); if (unlink("/etc/adjtime") < 0)
if (unlink("/etc/adjtime") < 0) {
if (errno != ENOENT) if (errno != ENOENT)
return -errno; return -errno;
}
return 0; return 0;
} }
} }
label_init("/etc"); label_init("/etc");
r = write_string_file_atomic_label("/etc/adjtime", w); return write_string_file_atomic_label("/etc/adjtime", w);
free(w);
return r;
} }
static char** get_ntp_services(void) { static char** get_ntp_services(void) {
char **r = NULL, **files, **i; char _cleanup_strv_free_ **r = NULL, **files;
char **i;
int k; int k;
k = conf_files_list(&files, ".list", NULL, k = conf_files_list(&files, ".list", NULL,
@ -295,14 +282,14 @@ static char** get_ntp_services(void) {
return NULL; return NULL;
STRV_FOREACH(i, files) { STRV_FOREACH(i, files) {
FILE *f; FILE _cleanup_fclose_ *f;
f = fopen(*i, "re"); f = fopen(*i, "re");
if (!f) if (!f)
continue; continue;
for (;;) { for (;;) {
char line[PATH_MAX], *l, **q; char line[PATH_MAX], *l;
if (!fgets(line, sizeof(line), f)) { if (!fgets(line, sizeof(line), f)) {
@ -316,22 +303,16 @@ static char** get_ntp_services(void) {
if (l[0] == 0 || l[0] == '#') if (l[0] == 0 || l[0] == '#')
continue; continue;
q = strv_append(r, l); if (strv_extend(&r, l) < 0)
if (!q) {
log_oom(); log_oom();
break; return NULL;
}
strv_free(r);
r = q;
} }
fclose(f);
} }
strv_free(files); i = r;
r = NULL; /* avoid cleanup */
return strv_uniq(r); return strv_uniq(i);
} }
static int read_ntp(DBusConnection *bus) { static int read_ntp(DBusConnection *bus) {