Skip to content

Commit 2aa2860

Browse files
committed
gpt-auto-generator: use stack variables and fix minor memleak
1 parent b29f648 commit 2aa2860

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

src/gpt-auto-generator/gpt-auto-generator.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ static bool arg_root_enabled = true;
4646
static bool arg_root_rw = false;
4747

4848
static int add_cryptsetup(const char *id, const char *what, bool rw, bool require, char **device) {
49-
_cleanup_free_ char *e = NULL, *n = NULL, *p = NULL, *d = NULL, *id_escaped = NULL, *what_escaped = NULL;
49+
_cleanup_free_ char *e = NULL, *n = NULL, *d = NULL, *id_escaped = NULL, *what_escaped = NULL;
5050
_cleanup_fclose_ FILE *f = NULL;
51-
char *ret;
51+
const char *p;
5252
int r;
5353

5454
assert(id);
@@ -74,10 +74,7 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, bool requir
7474
if (!what_escaped)
7575
return log_oom();
7676

77-
p = strjoin(arg_dest, "/", n);
78-
if (!p)
79-
return log_oom();
80-
77+
p = strjoina(arg_dest, "/", n);
8178
f = fopen(p, "wxe");
8279
if (!f)
8380
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
125122
return r;
126123
}
127124

128-
free(p);
129-
p = strjoin(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf");
130-
if (!p)
131-
return log_oom();
132-
125+
p = strjoina(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf");
133126
mkdir_parents_label(p, 0755);
134127
r = write_string_file(p,
135128
"# 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
139132
if (r < 0)
140133
return log_error_errno(r, "Failed to write device drop-in: %m");
141134

142-
ret = strappend("/dev/mapper/", id);
143-
if (!ret)
144-
return log_oom();
135+
if (device) {
136+
char *ret;
137+
138+
ret = strappend("/dev/mapper/", id);
139+
if (!ret)
140+
return log_oom();
145141

146-
if (device)
147142
*device = ret;
143+
}
144+
148145
return 0;
149146
}
150147

@@ -337,20 +334,16 @@ static int add_automount(
337334
usec_t timeout) {
338335

339336
_cleanup_free_ char *unit = NULL;
340-
_cleanup_free_ char *opt, *p = NULL;
341337
_cleanup_fclose_ FILE *f = NULL;
338+
const char *opt = "noauto", *p;
342339
int r;
343340

344341
assert(id);
345342
assert(where);
346343
assert(description);
347344

348345
if (options)
349-
opt = strjoin(options, ",noauto");
350-
else
351-
opt = strdup("noauto");
352-
if (!opt)
353-
return log_oom();
346+
opt = strjoina(options, ",", opt);
354347

355348
r = add_mount(id,
356349
what,
@@ -367,10 +360,7 @@ static int add_automount(
367360
if (r < 0)
368361
return log_error_errno(r, "Failed to generate unit name: %m");
369362

370-
p = strjoin(arg_dest, "/", unit);
371-
if (!p)
372-
return log_oom();
373-
363+
p = strjoina(arg_dest, "/", unit);
374364
f = fopen(p, "wxe");
375365
if (!f)
376366
return log_error_errno(errno, "Failed to create unit file %s: %m", unit);

0 commit comments

Comments
 (0)