1
0
mirror of https://github.com/systemd/systemd synced 2024-07-03 08:29:25 +00:00

Replace strdup_or_null() by strdup_to()

I didn't know that this helper existed… It is very similar to strdup_to_full(),
but all callers can actually be replaced by strdup_to(), which has more fitting
semantics.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2024-03-19 21:08:52 +01:00
parent f174b294f6
commit 6a705f1234
7 changed files with 17 additions and 43 deletions

View File

@ -303,25 +303,4 @@ bool version_is_valid_versionspec(const char *s);
ssize_t strlevenshtein(const char *x, const char *y);
static inline int strdup_or_null(const char *s, char **ret) {
char *c;
assert(ret);
/* This is a lot like strdup(), but is happy with NULL strings, and does not treat that as error, but
* copies the NULL value. */
if (!s) {
*ret = NULL;
return 0;
}
c = strdup(s);
if (!c)
return -ENOMEM;
*ret = c;
return 1;
}
char *strrstr(const char *haystack, const char *needle);

View File

@ -5338,7 +5338,7 @@ int unit_set_exec_params(Unit *u, ExecParameters *p) {
p->runtime_scope = u->manager->runtime_scope;
r = strdup_or_null(manager_get_confirm_spawn(u->manager), &p->confirm_spawn);
r = strdup_to(&p->confirm_spawn, manager_get_confirm_spawn(u->manager));
if (r < 0)
return r;

View File

@ -498,13 +498,8 @@ static int mandatory_mount_drop_unapplicable_options(
assert(where);
assert(ret_options);
if (!(*flags & (MOUNT_NOAUTO|MOUNT_NOFAIL|MOUNT_AUTOMOUNT))) {
r = strdup_or_null(options, ret_options);
if (r < 0)
return r;
return 0;
}
if (!(*flags & (MOUNT_NOAUTO|MOUNT_NOFAIL|MOUNT_AUTOMOUNT)))
return strdup_to(ret_options, options);
log_debug("Mount '%s' is mandatory, ignoring 'noauto', 'nofail', and 'x-systemd.automount' options.",
where);

View File

@ -151,37 +151,37 @@ static int context_copy(const Context *source, Context *ret) {
return copy.rfd;
}
r = strdup_or_null(source->layout_other, &copy.layout_other);
r = strdup_to(&copy.layout_other, source->layout_other);
if (r < 0)
return r;
r = strdup_or_null(source->conf_root, &copy.conf_root);
r = strdup_to(&copy.conf_root, source->conf_root);
if (r < 0)
return r;
r = strdup_or_null(source->boot_root, &copy.boot_root);
r = strdup_to(&copy.boot_root, source->boot_root);
if (r < 0)
return r;
r = strdup_or_null(source->entry_token, &copy.entry_token);
r = strdup_to(&copy.entry_token, source->entry_token);
if (r < 0)
return r;
r = strdup_or_null(source->entry_dir, &copy.entry_dir);
r = strdup_to(&copy.entry_dir, source->entry_dir);
if (r < 0)
return r;
r = strdup_or_null(source->version, &copy.version);
r = strdup_to(&copy.version, source->version);
if (r < 0)
return r;
r = strdup_or_null(source->kernel, &copy.kernel);
r = strdup_to(&copy.kernel, source->kernel);
if (r < 0)
return r;
r = strv_copy_unless_empty(source->initrds, &copy.initrds);
if (r < 0)
return r;
r = strdup_or_null(source->initrd_generator, &copy.initrd_generator);
r = strdup_to(&copy.initrd_generator, source->initrd_generator);
if (r < 0)
return r;
r = strdup_or_null(source->uki_generator, &copy.uki_generator);
r = strdup_to(&copy.uki_generator, source->uki_generator);
if (r < 0)
return r;
r = strdup_or_null(source->staging_area, &copy.staging_area);
r = strdup_to(&copy.staging_area, source->staging_area);
if (r < 0)
return r;
r = strv_copy_unless_empty(source->plugins, &copy.plugins);

View File

@ -613,12 +613,12 @@ int address_dup(const Address *src, Address **ret) {
dest->nft_set_context.n_sets = 0;
if (src->family == AF_INET) {
r = strdup_or_null(src->label, &dest->label);
r = strdup_to(&dest->label, src->label);
if (r < 0)
return r;
}
r = strdup_or_null(src->netlabel, &dest->netlabel);
r = strdup_to(&dest->netlabel, src->netlabel);
if (r < 0)
return r;

View File

@ -39,7 +39,7 @@ int route_metric_copy(const RouteMetric *src, RouteMetric *dest) {
} else
dest->metrics_set = NULL;
return strdup_or_null(src->tcp_congestion_control_algo, &dest->tcp_congestion_control_algo);
return strdup_to(&dest->tcp_congestion_control_algo, src->tcp_congestion_control_algo);
}
void route_metric_hash_func(const RouteMetric *metric, struct siphash *state) {

View File

@ -218,7 +218,7 @@ static int route_nexthop_copy(const RouteNextHop *src, RouteNextHop *dest) {
/* unset pointer copied in the above. */
dest->ifname = NULL;
return strdup_or_null(src->ifindex > 0 ? NULL : src->ifname, &dest->ifname);
return strdup_to(&dest->ifname, src->ifindex > 0 ? NULL : src->ifname);
}
static int route_nexthop_dup(const RouteNextHop *src, RouteNextHop **ret) {