Skip to content

Commit

Permalink
tree-wide: drop NULL sentinel from strjoin
Browse files Browse the repository at this point in the history
This makes strjoin and strjoina more similar and avoids the useless final
argument.

spatch -I . -I ./src -I ./src/basic -I ./src/basic -I ./src/shared -I ./src/shared -I ./src/network -I ./src/locale -I ./src/login -I ./src/journal -I ./src/journal -I ./src/timedate -I ./src/timesync -I ./src/nspawn -I ./src/resolve -I ./src/resolve -I ./src/systemd -I ./src/core -I ./src/core -I ./src/libudev -I ./src/udev -I ./src/udev/net -I ./src/udev -I ./src/libsystemd/sd-bus -I ./src/libsystemd/sd-event -I ./src/libsystemd/sd-login -I ./src/libsystemd/sd-netlink -I ./src/libsystemd/sd-network -I ./src/libsystemd/sd-hwdb -I ./src/libsystemd/sd-device -I ./src/libsystemd/sd-id128 -I ./src/libsystemd-network --sp-file coccinelle/strjoin.cocci --in-place $(git ls-files src/*.c)

git grep -e '\bstrjoin\b.*NULL' -l|xargs sed -i -r 's/strjoin\((.*), NULL\)/strjoin(\1)/'

This might have missed a few cases (spatch has a really hard time dealing
with _cleanup_ macros), but that's no big issue, they can always be fixed
later.
  • Loading branch information
keszybz committed Oct 23, 2016
1 parent 3c5d7c9 commit 605405c
Show file tree
Hide file tree
Showing 89 changed files with 215 additions and 194 deletions.
16 changes: 16 additions & 0 deletions coccinelle/strjoin.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@@
expression list args;
@@
- strjoin(args, NULL);
+ strjoin(args);
@@
expression t;
expression list args;
@@
- t = strjoin(args, NULL);
+ t = strjoin(args);
@@
expression list args;
@@
- return strjoin(args, NULL);
+ return strjoin(args);
4 changes: 2 additions & 2 deletions src/backlight/backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

saved = strjoin("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_ss, ":", escaped_sysname, NULL);
saved = strjoin("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_ss, ":", escaped_sysname);
} else
saved = strjoin("/var/lib/systemd/backlight/", escaped_ss, ":", escaped_sysname, NULL);
saved = strjoin("/var/lib/systemd/backlight/", escaped_ss, ":", escaped_sysname);

if (!saved) {
log_oom();
Expand Down
2 changes: 1 addition & 1 deletion src/basic/btrfs-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ static int subvol_snapshot_children(int old_fd, int new_fd, const char *subvolum
if (old_child_fd < 0)
return -errno;

np = strjoin(subvolume, "/", ino_args.name, NULL);
np = strjoin(subvolume, "/", ino_args.name);
if (!np)
return -ENOMEM;

Expand Down
16 changes: 8 additions & 8 deletions src/basic/cgroup-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ int cg_kill_recursive(
while ((r = cg_read_subgroup(d, &fn)) > 0) {
_cleanup_free_ char *p = NULL;

p = strjoin(path, "/", fn, NULL);
p = strjoin(path, "/", fn);
free(fn);
if (!p)
return -ENOMEM;
Expand Down Expand Up @@ -479,7 +479,7 @@ int cg_migrate_recursive(
while ((r = cg_read_subgroup(d, &fn)) > 0) {
_cleanup_free_ char *p = NULL;

p = strjoin(pfrom, "/", fn, NULL);
p = strjoin(pfrom, "/", fn);
free(fn);
if (!p)
return -ENOMEM;
Expand Down Expand Up @@ -562,11 +562,11 @@ static int join_path_legacy(const char *controller, const char *path, const char
if (isempty(path) && isempty(suffix))
t = strappend("/sys/fs/cgroup/", dn);
else if (isempty(path))
t = strjoin("/sys/fs/cgroup/", dn, "/", suffix, NULL);
t = strjoin("/sys/fs/cgroup/", dn, "/", suffix);
else if (isempty(suffix))
t = strjoin("/sys/fs/cgroup/", dn, "/", path, NULL);
t = strjoin("/sys/fs/cgroup/", dn, "/", path);
else
t = strjoin("/sys/fs/cgroup/", dn, "/", path, "/", suffix, NULL);
t = strjoin("/sys/fs/cgroup/", dn, "/", path, "/", suffix);
if (!t)
return -ENOMEM;

Expand All @@ -586,7 +586,7 @@ static int join_path_unified(const char *path, const char *suffix, char **fs) {
else if (isempty(suffix))
t = strappend("/sys/fs/cgroup/", path);
else
t = strjoin("/sys/fs/cgroup/", path, "/", suffix, NULL);
t = strjoin("/sys/fs/cgroup/", path, "/", suffix);
if (!t)
return -ENOMEM;

Expand All @@ -613,7 +613,7 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch
else if (!path)
t = strdup(suffix);
else
t = strjoin(path, "/", suffix, NULL);
t = strjoin(path, "/", suffix);
if (!t)
return -ENOMEM;

Expand Down Expand Up @@ -1145,7 +1145,7 @@ int cg_is_empty_recursive(const char *controller, const char *path) {
while ((r = cg_read_subgroup(d, &fn)) > 0) {
_cleanup_free_ char *p = NULL;

p = strjoin(path, "/", fn, NULL);
p = strjoin(path, "/", fn);
free(fn);
if (!p)
return -ENOMEM;
Expand Down
2 changes: 1 addition & 1 deletion src/basic/conf-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static int files_add(Hashmap *h, const char *root, const char *path, const char
if (!dirent_is_file_with_suffix(de, suffix))
continue;

p = strjoin(dirpath, "/", de->d_name, NULL);
p = strjoin(dirpath, "/", de->d_name);
if (!p)
return -ENOMEM;

Expand Down
6 changes: 3 additions & 3 deletions src/basic/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ static int load_env_file_push(
return -EINVAL;
}

p = strjoin(key, "=", strempty(value), NULL);
p = strjoin(key, "=", strempty(value));
if (!p)
return -ENOMEM;

Expand Down Expand Up @@ -963,9 +963,9 @@ static int search_and_fopen_internal(const char *path, const char *mode, const c
FILE *f;

if (root)
p = strjoin(root, *i, "/", path, NULL);
p = strjoin(root, *i, "/", path);
else
p = strjoin(*i, "/", path, NULL);
p = strjoin(*i, "/", path);
if (!p)
return -ENOMEM;

Expand Down
2 changes: 1 addition & 1 deletion src/basic/fs-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ int chase_symlinks(const char *path, const char *_root, char **ret) {
/* A relative destination. If so, this is what we'll prefix what's left to do with what
* we just read, and start the loop again, but remain in the current directory. */

joined = strjoin("/", destination, todo, NULL);
joined = strjoin("/", destination, todo);
if (!joined)
return -ENOMEM;

Expand Down
2 changes: 1 addition & 1 deletion src/basic/mount-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ static char* mount_flags_to_string(long unsigned flags) {
FLAG(MS_I_VERSION),
FLAG(MS_STRICTATIME),
FLAG(MS_LAZYTIME),
y, NULL);
y);
if (!x)
return NULL;
if (!y)
Expand Down
12 changes: 5 additions & 7 deletions src/basic/path-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ char *path_make_absolute(const char *p, const char *prefix) {
if (path_is_absolute(p) || !prefix)
return strdup(p);

return strjoin(prefix, "/", p, NULL);
return strjoin(prefix, "/", p);
}

int path_make_absolute_cwd(const char *p, char **ret) {
Expand All @@ -104,7 +104,7 @@ int path_make_absolute_cwd(const char *p, char **ret) {
if (!cwd)
return negative_errno();

c = strjoin(cwd, "/", p, NULL);
c = strjoin(cwd, "/", p);
}
if (!c)
return -ENOMEM;
Expand Down Expand Up @@ -444,13 +444,11 @@ char* path_join(const char *root, const char *path, const char *rest) {
return strjoin(root, endswith(root, "/") ? "" : "/",
path[0] == '/' ? path+1 : path,
rest ? (endswith(path, "/") ? "" : "/") : NULL,
rest && rest[0] == '/' ? rest+1 : rest,
NULL);
rest && rest[0] == '/' ? rest+1 : rest);
else
return strjoin(path,
rest ? (endswith(path, "/") ? "" : "/") : NULL,
rest && rest[0] == '/' ? rest+1 : rest,
NULL);
rest && rest[0] == '/' ? rest+1 : rest);
}

int find_binary(const char *name, char **ret) {
Expand Down Expand Up @@ -494,7 +492,7 @@ int find_binary(const char *name, char **ret) {
if (!path_is_absolute(element))
continue;

j = strjoin(element, "/", name, NULL);
j = strjoin(element, "/", name);
if (!j)
return -ENOMEM;

Expand Down
6 changes: 3 additions & 3 deletions src/basic/process-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,14 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
return h;

if (max_length == 0)
r = strjoin("[", t, "]", NULL);
r = strjoin("[", t, "]");
else {
size_t l;

l = strlen(t);

if (l + 3 <= max_length)
r = strjoin("[", t, "]", NULL);
r = strjoin("[", t, "]");
else if (max_length <= 6) {

r = new(char, max_length);
Expand All @@ -263,7 +263,7 @@ int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char *
e--;
*e = 0;

r = strjoin("[", t, "...]", NULL);
r = strjoin("[", t, "...]");
}
}
if (!r)
Expand Down
2 changes: 1 addition & 1 deletion src/basic/string-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ char *strappend(const char *s, const char *suffix) {
return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
}

char *strjoin(const char *x, ...) {
char *strjoin_real(const char *x, ...) {
va_list ap;
size_t l;
char *r, *p;
Expand Down
3 changes: 2 additions & 1 deletion src/basic/string-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ const char* split(const char **state, size_t *l, const char *separator, bool quo
char *strappend(const char *s, const char *suffix);
char *strnappend(const char *s, const char *suffix, size_t length);

char *strjoin(const char *x, ...) _sentinel_;
char *strjoin_real(const char *x, ...) _sentinel_;
#define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)

#define strjoina(a, ...) \
({ \
Expand Down
4 changes: 2 additions & 2 deletions src/basic/unit-name.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ int unit_name_build(const char *prefix, const char *instance, const char *suffix
if (!instance)
s = strappend(prefix, suffix);
else
s = strjoin(prefix, "@", instance, suffix, NULL);
s = strjoin(prefix, "@", instance, suffix);
if (!s)
return -ENOMEM;

Expand Down Expand Up @@ -554,7 +554,7 @@ int unit_name_from_path_instance(const char *prefix, const char *path, const cha
if (r < 0)
return r;

s = strjoin(prefix, "@", p, suffix, NULL);
s = strjoin(prefix, "@", p, suffix);
if (!s)
return -ENOMEM;

Expand Down
2 changes: 1 addition & 1 deletion src/basic/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ static int do_execute(char **directories, usec_t timeout, char *argv[]) {
if (r < 0)
return log_oom();

path = strjoin(*directory, "/", de->d_name, NULL);
path = strjoin(*directory, "/", de->d_name);
if (!path)
return log_oom();

Expand Down
2 changes: 1 addition & 1 deletion src/cgls/cgls.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ int main(int argc, char *argv[]) {

controller = c ?: SYSTEMD_CGROUP_CONTROLLER;
if (p) {
j = strjoin(root, "/", p, NULL);
j = strjoin(root, "/", p);
if (!j) {
r = log_oom();
goto finish;
Expand Down
2 changes: 1 addition & 1 deletion src/cgtop/cgtop.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ static int refresh_one(
if (r == 0)
break;

p = strjoin(path, "/", fn, NULL);
p = strjoin(path, "/", fn);
if (!p)
return -ENOMEM;

Expand Down
7 changes: 4 additions & 3 deletions src/core/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,9 +1201,10 @@ char *unit_default_cgroup_path(Unit *u) {
return NULL;

if (slice)
return strjoin(u->manager->cgroup_root, "/", slice, "/", escaped, NULL);
return strjoin(u->manager->cgroup_root, "/", slice, "/",
escaped);
else
return strjoin(u->manager->cgroup_root, "/", escaped, NULL);
return strjoin(u->manager->cgroup_root, "/", escaped);
}

int unit_set_cgroup_path(Unit *u, const char *path) {
Expand Down Expand Up @@ -1643,7 +1644,7 @@ static int unit_watch_pids_in_path(Unit *u, const char *path) {
while ((r = cg_read_subgroup(d, &fn)) > 0) {
_cleanup_free_ char *p = NULL;

p = strjoin(path, "/", fn, NULL);
p = strjoin(path, "/", fn);
free(fn);

if (!p)
Expand Down
2 changes: 1 addition & 1 deletion src/core/dbus-execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ int bus_exec_context_set_transient_property(
if (mode != UNIT_CHECK) {
char *buf = NULL;

buf = strjoin(b ? "-" : "", path, NULL);
buf = strjoin(b ? "-" : "", path);
if (!buf)
return -ENOMEM;

Expand Down
6 changes: 3 additions & 3 deletions src/core/dbus-unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ int bus_unit_method_start_generic(
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode);

if (reload_if_possible)
verb = strjoin("reload-or-", job_type_to_string(job_type), NULL);
verb = strjoin("reload-or-", job_type_to_string(job_type));
else
verb = strdup(job_type_to_string(job_type));
if (!verb)
Expand Down Expand Up @@ -986,7 +986,7 @@ static int append_cgroup(sd_bus_message *reply, const char *p, Set *pids) {
if (r == 0)
break;

j = strjoin(p, "/", g, NULL);
j = strjoin(p, "/", g);
if (!j)
return -ENOMEM;

Expand Down Expand Up @@ -1365,7 +1365,7 @@ static int bus_unit_set_transient_property(
if (r < 0)
return r;

label = strjoin(name, "-", other, NULL);
label = strjoin(name, "-", other);
if (!label)
return -ENOMEM;

Expand Down
2 changes: 1 addition & 1 deletion src/core/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ static int device_update_description(Unit *u, struct udev_device *dev, const cha
if (label) {
_cleanup_free_ char *j;

j = strjoin(model, " ", label, NULL);
j = strjoin(model, " ", label);
if (j)
r = unit_set_description(u, j);
else
Expand Down
10 changes: 5 additions & 5 deletions src/core/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ static int build_environment(
if (!joined)
return -ENOMEM;

x = strjoin("LISTEN_FDNAMES=", joined, NULL);
x = strjoin("LISTEN_FDNAMES=", joined);
if (!x)
return -ENOMEM;
our_env[n_env++] = x;
Expand Down Expand Up @@ -1737,7 +1737,7 @@ static int build_pass_environment(const ExecContext *c, char ***ret) {
v = getenv(*i);
if (!v)
continue;
x = strjoin(*i, "=", v, NULL);
x = strjoin(*i, "=", v);
if (!x)
return -ENOMEM;
if (!GREEDY_REALLOC(pass_env, n_bufsize, n_env + 2))
Expand Down Expand Up @@ -1951,7 +1951,7 @@ static int setup_runtime_directory(
STRV_FOREACH(rt, context->runtime_directory) {
_cleanup_free_ char *p;

p = strjoin(params->runtime_prefix, "/", *rt, NULL);
p = strjoin(params->runtime_prefix, "/", *rt);
if (!p)
return -ENOMEM;

Expand Down Expand Up @@ -2027,7 +2027,7 @@ static int compile_read_write_paths(
STRV_FOREACH(rt, context->runtime_directory) {
char *s;

s = strjoin(params->runtime_prefix, "/", *rt, NULL);
s = strjoin(params->runtime_prefix, "/", *rt);
if (!s)
return -ENOMEM;

Expand Down Expand Up @@ -3006,7 +3006,7 @@ int exec_context_destroy_runtime_directory(ExecContext *c, const char *runtime_p
STRV_FOREACH(i, c->runtime_directory) {
_cleanup_free_ char *p;

p = strjoin(runtime_prefix, "/", *i, NULL);
p = strjoin(runtime_prefix, "/", *i);
if (!p)
return -ENOMEM;

Expand Down
2 changes: 1 addition & 1 deletion src/core/locale-setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int locale_setup(char ***environment) {
if (!variables[i])
continue;

s = strjoin(locale_variable_to_string(i), "=", variables[i], NULL);
s = strjoin(locale_variable_to_string(i), "=", variables[i]);
if (!s) {
r = -ENOMEM;
goto finish;
Expand Down
Loading

0 comments on commit 605405c

Please sign in to comment.