summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mack <daniel@zonque.org>2015-07-29 20:25:57 +0200
committerDaniel Mack <daniel@zonque.org>2015-07-30 13:09:01 +0200
commit2fc09a9cdd1ad25bc7c53a23d5301eb952e1ce3d (patch)
treec662c5bf209dae93822ac07a831882a7ba4ba0b2
parent5f17089ac1e86c25806d3f066243561360efd2ca (diff)
tree-wide: use free_and_strdup()
Use free_and_strdup() where appropriate and replace equivalent, open-coded versions.
-rw-r--r--src/basic/unit-name.c9
-rw-r--r--src/firstboot/firstboot.c26
-rw-r--r--src/gpt-auto-generator/gpt-auto-generator.c15
-rw-r--r--src/libsystemd/sd-bus/sd-bus.c7
-rw-r--r--src/login/loginctl.c8
-rw-r--r--src/network/networkd-link.c7
-rw-r--r--src/nspawn/nspawn.c5
-rw-r--r--src/run/run.c8
-rw-r--r--src/sysusers/sysusers.c11
-rw-r--r--src/udev/udev-rules.c3
10 files changed, 40 insertions, 59 deletions
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
index bf52463d81..4b84542d0c 100644
--- a/src/basic/unit-name.c
+++ b/src/basic/unit-name.c
@@ -673,6 +673,7 @@ int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, c
int slice_build_parent_slice(const char *slice, char **ret) {
char *s, *dash;
+ int r;
assert(slice);
assert(ret);
@@ -693,11 +694,9 @@ int slice_build_parent_slice(const char *slice, char **ret) {
if (dash)
strcpy(dash, ".slice");
else {
- free(s);
-
- s = strdup("-.slice");
- if (!s)
- return -ENOMEM;
+ r = free_and_strdup(&s, "-.slice");
+ if (r < 0)
+ return r;
}
*ret = s;
diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c
index 3805b29437..f06ab3da29 100644
--- a/src/firstboot/firstboot.c
+++ b/src/firstboot/firstboot.c
@@ -728,9 +728,8 @@ static int parse_argv(int argc, char *argv[]) {
return -EINVAL;
}
- free(arg_locale);
- arg_locale = strdup(optarg);
- if (!arg_locale)
+ r = free_and_strdup(&arg_locale, optarg);
+ if (r < 0)
return log_oom();
break;
@@ -741,9 +740,8 @@ static int parse_argv(int argc, char *argv[]) {
return -EINVAL;
}
- free(arg_locale_messages);
- arg_locale_messages = strdup(optarg);
- if (!arg_locale_messages)
+ r = free_and_strdup(&arg_locale_messages, optarg);
+ if (r < 0)
return log_oom();
break;
@@ -754,19 +752,16 @@ static int parse_argv(int argc, char *argv[]) {
return -EINVAL;
}
- free(arg_timezone);
- arg_timezone = strdup(optarg);
- if (!arg_timezone)
+ r = free_and_strdup(&arg_timezone, optarg);
+ if (r < 0)
return log_oom();
break;
case ARG_ROOT_PASSWORD:
- free(arg_root_password);
- arg_root_password = strdup(optarg);
- if (!arg_root_password)
+ r = free_and_strdup(&arg_root_password, optarg);
+ if (r < 0)
return log_oom();
-
break;
case ARG_ROOT_PASSWORD_FILE:
@@ -785,9 +780,8 @@ static int parse_argv(int argc, char *argv[]) {
return -EINVAL;
}
- free(arg_hostname);
- arg_hostname = strdup(optarg);
- if (!arg_hostname)
+ r = free_and_strdup(&arg_hostname, optarg);
+ if (r < 0)
return log_oom();
break;
diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c
index 9fc576fb45..50acb7595c 100644
--- a/src/gpt-auto-generator/gpt-auto-generator.c
+++ b/src/gpt-auto-generator/gpt-auto-generator.c
@@ -727,9 +727,8 @@ static int enumerate_partitions(dev_t devnum) {
boot_nr = nr;
- free(boot);
- boot = strdup(subnode);
- if (!boot)
+ r = free_and_strdup(&boot, subnode);
+ if (r < 0)
return log_oom();
} else if (sd_id128_equal(type_id, GPT_HOME)) {
@@ -741,9 +740,8 @@ static int enumerate_partitions(dev_t devnum) {
home_nr = nr;
home_rw = !(flags & GPT_FLAG_READ_ONLY),
- free(home);
- home = strdup(subnode);
- if (!home)
+ r = free_and_strdup(&home, subnode);
+ if (r < 0)
return log_oom();
} else if (sd_id128_equal(type_id, GPT_SRV)) {
@@ -755,9 +753,8 @@ static int enumerate_partitions(dev_t devnum) {
srv_nr = nr;
srv_rw = !(flags & GPT_FLAG_READ_ONLY),
- free(srv);
- srv = strdup(subnode);
- if (!srv)
+ r = free_and_strdup(&srv, subnode);
+ if (r < 0)
return log_oom();
}
}
diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c
index 0ca225c617..767df40e81 100644
--- a/src/libsystemd/sd-bus/sd-bus.c
+++ b/src/libsystemd/sd-bus/sd-bus.c
@@ -896,10 +896,9 @@ static int parse_container_kernel_address(sd_bus *b, const char **p, char **guid
} else
b->nspid = 0;
- free(b->kernel);
- b->kernel = strdup("/sys/fs/kdbus/0-system/bus");
- if (!b->kernel)
- return -ENOMEM;
+ r = free_and_strdup(&b->kernel, "/sys/fs/kdbus/0-system/bus");
+ if (r < 0)
+ return r;
return 0;
}
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
index 9709eca9bd..5fa98e069f 100644
--- a/src/login/loginctl.c
+++ b/src/login/loginctl.c
@@ -372,11 +372,9 @@ static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_mess
if (r < 0)
return r;
- free(*p);
- *p = strdup(s);
-
- if (!*p)
- return -ENOMEM;
+ r = free_and_strdup(p, s);
+ if (r < 0)
+ return r;
} else {
r = sd_bus_message_read_basic(m, contents[0], userdata);
if (r < 0)
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 9d570e0146..78e96c4e5b 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -2059,10 +2059,9 @@ int link_update(Link *link, sd_netlink_message *m) {
link_free_carrier_maps(link);
- free(link->ifname);
- link->ifname = strdup(ifname);
- if (!link->ifname)
- return -ENOMEM;
+ r = free_and_strdup(&link->ifname, ifname);
+ if (r < 0)
+ return r;
r = link_new_carrier_maps(link);
if (r < 0)
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 65b9a5071b..d46f768cfa 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -503,9 +503,8 @@ static int parse_argv(int argc, char *argv[]) {
break;
case 'u':
- free(arg_user);
- arg_user = strdup(optarg);
- if (!arg_user)
+ r = free_and_strdup(&arg_user, optarg);
+ if (r < 0)
return log_oom();
break;
diff --git a/src/run/run.c b/src/run/run.c
index 148854a9b5..3dd97022de 100644
--- a/src/run/run.c
+++ b/src/run/run.c
@@ -1129,13 +1129,9 @@ int main(int argc, char* argv[]) {
}
if (arg_unit && isempty(description)) {
- free(description);
- description = strdup(arg_unit);
-
- if (!description) {
- r = log_oom();
+ r = free_and_strdup(&description, arg_unit);
+ if (r < 0)
goto finish;
- }
}
arg_description = description;
diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c
index d7ba482834..b3fa29b84c 100644
--- a/src/sysusers/sysusers.c
+++ b/src/sysusers/sysusers.c
@@ -891,8 +891,10 @@ static int add_user(Item *i) {
i->uid = p->pw_uid;
i->uid_set = true;
- free(i->description);
- i->description = strdup(p->pw_gecos);
+ r = free_and_strdup(&i->description, p->pw_gecos);
+ if (r < 0)
+ return log_oom();
+
return 0;
}
if (!IN_SET(errno, 0, ENOENT))
@@ -1149,9 +1151,8 @@ static int process_item(Item *i) {
}
if (i->gid_path) {
- free(j->gid_path);
- j->gid_path = strdup(i->gid_path);
- if (!j->gid_path)
+ r = free_and_strdup(&j->gid_path, i->gid_path);
+ if (r < 0)
return log_oom();
}
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
index d00f90afa6..bbb9f97226 100644
--- a/src/udev/udev-rules.c
+++ b/src/udev/udev-rules.c
@@ -2429,8 +2429,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules,
rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
break;
}
- free(event->name);
- event->name = strdup(name_str);
+ free_and_strdup(&event->name, name_str);
log_debug("NAME '%s' %s:%u",
event->name,
rules_str(rules, rule->rule.filename_off),