Merge pull request #8812 from keszybz/gpt-auto-memleak

gpt-auto-generator: use stack variables and fix minor memleak
This commit is contained in:
Lennart Poettering 2018-04-25 15:46:57 +02:00 committed by GitHub
commit 2de2337518
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 38 deletions

View File

@ -46,9 +46,9 @@ static bool arg_root_enabled = true;
static bool arg_root_rw = false;
static int add_cryptsetup(const char *id, const char *what, bool rw, bool require, char **device) {
_cleanup_free_ char *e = NULL, *n = NULL, *p = NULL, *d = NULL, *id_escaped = NULL, *what_escaped = NULL;
_cleanup_free_ char *e = NULL, *n = NULL, *d = NULL, *id_escaped = NULL, *what_escaped = NULL;
_cleanup_fclose_ FILE *f = NULL;
char *ret;
const char *p;
int r;
assert(id);
@ -74,10 +74,7 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, bool requir
if (!what_escaped)
return log_oom();
p = strjoin(arg_dest, "/", n);
if (!p)
return log_oom();
p = strjoina(arg_dest, "/", n);
f = fopen(p, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m", p);
@ -125,11 +122,7 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, bool requir
return r;
}
free(p);
p = strjoin(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf");
if (!p)
return log_oom();
p = strjoina(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf");
mkdir_parents_label(p, 0755);
r = write_string_file(p,
"# Automatically generated by systemd-gpt-auto-generator\n\n"
@ -139,12 +132,16 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, bool requir
if (r < 0)
return log_error_errno(r, "Failed to write device drop-in: %m");
ret = strappend("/dev/mapper/", id);
if (!ret)
return log_oom();
if (device) {
char *ret;
ret = strappend("/dev/mapper/", id);
if (!ret)
return log_oom();
if (device)
*device = ret;
}
return 0;
}
@ -233,7 +230,7 @@ static int add_mount(
return 0;
}
static bool path_is_busy(const char *where) {
static int path_is_busy(const char *where) {
int r;
/* already a mountpoint; generators run during reload */
@ -246,13 +243,17 @@ static bool path_is_busy(const char *where) {
return false;
if (r < 0)
return true;
return log_warning_errno(r, "Cannot check if \"%s\" is a mount point: %m", where);
/* not a mountpoint but it contains files */
if (dir_is_empty(where) <= 0)
return true;
r = dir_is_empty(where);
if (r < 0)
return log_warning_errno(r, "Cannot check if \"%s\" is empty: %m", where);
if (r > 0)
return false;
return false;
log_debug("\"%s\" already populated, ignoring.", where);
return true;
}
static int add_partition_mount(
@ -261,12 +262,12 @@ static int add_partition_mount(
const char *where,
const char *description) {
int r;
assert(p);
if (path_is_busy(where)) {
log_debug("%s already populated, ignoring.", where);
return 0;
}
r = path_is_busy(where);
if (r != 0)
return r < 0 ? r : 0;
return add_mount(
id,
@ -337,8 +338,8 @@ static int add_automount(
usec_t timeout) {
_cleanup_free_ char *unit = NULL;
_cleanup_free_ char *opt, *p = NULL;
_cleanup_fclose_ FILE *f = NULL;
const char *opt = "noauto", *p;
int r;
assert(id);
@ -346,11 +347,7 @@ static int add_automount(
assert(description);
if (options)
opt = strjoin(options, ",noauto");
else
opt = strdup("noauto");
if (!opt)
return log_oom();
opt = strjoina(options, ",", opt);
r = add_mount(id,
what,
@ -367,10 +364,7 @@ static int add_automount(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
p = strjoin(arg_dest, "/", unit);
if (!p)
return log_oom();
p = strjoina(arg_dest, "/", unit);
f = fopen(p, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
@ -417,10 +411,9 @@ static int add_esp(DissectedPartition *p) {
return 0;
}
if (path_is_busy(esp)) {
log_debug("%s already populated, ignoring.", esp);
return 0;
}
r = path_is_busy(esp);
if (r != 0)
return r < 0 ? r : 0;
if (is_efi_boot()) {
sd_id128_t loader_uuid;