]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: imply NULL termination of strextend() argument list
authorLennart Poettering <lennart@poettering.net>
Tue, 5 Jan 2021 14:03:41 +0000 (15:03 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 6 Jan 2021 16:24:46 +0000 (17:24 +0100)
The trailing NULL in the argument list is now implied (similar to
what we already have in place in strjoin()).

23 files changed:
src/basic/cgroup-util.c
src/basic/fs-util.c
src/basic/string-util.c
src/basic/string-util.h
src/busctl/busctl-introspect.c
src/core/device.c
src/core/killall.c
src/fstab-generator/fstab-generator.c
src/hibernate-resume/hibernate-resume-generator.c
src/libsystemd/sd-bus/bus-message.c
src/machine/machinectl.c
src/portable/portable.c
src/resolve/resolved-link-bus.c
src/shared/bus-unit-util.c
src/shared/conf-parser.c
src/shared/dissect-image.c
src/shared/mount-util.c
src/shared/nsflags.c
src/shutdown/umount.c
src/systemctl/systemctl-show.c
src/test/test-string-util.c
src/tmpfiles/tmpfiles.c
src/udev/udev-rules.c

index f28bf1866a0505c4f2fa77a0aef5288892537478..375fd1c3be39a7ce9fb965d2c2737b493c42a772 100644 (file)
@@ -1619,7 +1619,7 @@ int cg_slice_to_path(const char *unit, char **ret) {
                 if (!escaped)
                         return -ENOMEM;
 
-                if (!strextend(&s, escaped, "/", NULL))
+                if (!strextend(&s, escaped, "/"))
                         return -ENOMEM;
 
                 dash = strchr(dash+1, '-');
@@ -1629,7 +1629,7 @@ int cg_slice_to_path(const char *unit, char **ret) {
         if (!e)
                 return -ENOMEM;
 
-        if (!strextend(&s, e, NULL))
+        if (!strextend(&s, e))
                 return -ENOMEM;
 
         *ret = TAKE_PTR(s);
index f240f84322476c8a00f03a02236135e395f51bf2..7f44b93726b9a9aad351365a816cc7b7e1640b89 100644 (file)
@@ -934,7 +934,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
                         /* Preserve the trailing slash */
 
                         if (flags & CHASE_TRAIL_SLASH)
-                                if (!strextend(&done, "/", NULL))
+                                if (!strextend(&done, "/"))
                                         return -ENOMEM;
 
                         break;
@@ -1005,7 +1005,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
                                 if (streq_ptr(done, "/"))
                                         *done = '\0';
 
-                                if (!strextend(&done, first, todo, NULL))
+                                if (!strextend(&done, first, todo))
                                         return -ENOMEM;
 
                                 exists = false;
@@ -1098,7 +1098,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
                         if (streq(done, "/"))
                                 *done = '\0';
 
-                        if (!strextend(&done, first, NULL))
+                        if (!strextend(&done, first))
                                 return -ENOMEM;
                 }
 
index 7ab460faa5a277b2d66f8e85d4b16f5e8dd975c3..105952156d7512890a3e8fbb43ef66fbeb5e3127 100644 (file)
@@ -791,10 +791,10 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
         return *ibuf;
 }
 
-char *strextend_with_separator(char **x, const char *separator, ...) {
-        bool need_separator;
+char *strextend_with_separator_internal(char **x, const char *separator, ...) {
         size_t f, l, l_separator;
-        char *r, *p;
+        bool need_separator;
+        char *nr, *p;
         va_list ap;
 
         assert(x);
@@ -818,7 +818,7 @@ char *strextend_with_separator(char **x, const char *separator, ...) {
                 if (need_separator)
                         n += l_separator;
 
-                if (n > ((size_t) -1) - l) {
+                if (n >= SIZE_MAX - l) {
                         va_end(ap);
                         return NULL;
                 }
@@ -830,11 +830,12 @@ char *strextend_with_separator(char **x, const char *separator, ...) {
 
         need_separator = !isempty(*x);
 
-        r = realloc(*x, l+1);
-        if (!r)
+        nr = realloc(*x, l+1);
+        if (!nr)
                 return NULL;
 
-        p = r + f;
+        *x = nr;
+        p = nr + f;
 
         va_start(ap, separator);
         for (;;) {
@@ -853,12 +854,11 @@ char *strextend_with_separator(char **x, const char *separator, ...) {
         }
         va_end(ap);
 
-        assert(p == r + l);
+        assert(p == nr + l);
 
         *p = 0;
-        *x = r;
 
-        return r + l;
+        return p;
 }
 
 char *strrep(const char *s, unsigned n) {
index fdd3ce7363123fa257ecfadfefd0a9fde0c2931d..593cf04ae1d4fdec8455bca50cff25b178fe2716 100644 (file)
@@ -189,9 +189,10 @@ char *strreplace(const char *text, const char *old_string, const char *new_strin
 
 char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
 
-char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_;
+char *strextend_with_separator_internal(char **x, const char *separator, ...) _sentinel_;
 
-#define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__)
+#define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL)
+#define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL)
 
 char *strrep(const char *s, unsigned n);
 
index 7a5d57f8c83705b331213d088f2e2d65953899b5..89b32f4c73fddd1864ef1b89594b27723a3cbc04 100644 (file)
@@ -406,10 +406,10 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth
 
                                         if (argument_type) {
                                                 if (!argument_direction || streq(argument_direction, "in")) {
-                                                        if (!strextend(&context->member_signature, argument_type, NULL))
+                                                        if (!strextend(&context->member_signature, argument_type))
                                                                 return log_oom();
                                                 } else if (streq(argument_direction, "out")) {
-                                                        if (!strextend(&context->member_result, argument_type, NULL))
+                                                        if (!strextend(&context->member_result, argument_type))
                                                                 return log_oom();
                                                 } else
                                                         log_error("Unexpected method <arg> direction value '%s'.", argument_direction);
@@ -541,7 +541,7 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth
 
                                 if (argument_type) {
                                         if (!argument_direction || streq(argument_direction, "out")) {
-                                                if (!strextend(&context->member_signature, argument_type, NULL))
+                                                if (!strextend(&context->member_signature, argument_type))
                                                         return log_oom();
                                         } else
                                                 log_error("Unexpected signal <arg> direction value '%s'.", argument_direction);
index 6440c59e26eeea965344b6f4a6a49a9a656126d9..cbfb87a80863fa7716423c0c556df9d1b8de65e8 100644 (file)
@@ -201,7 +201,7 @@ static int device_found_to_string_many(DeviceFound flags, char **ret) {
                 if (!FLAGS_SET(flags, device_found_map[i].flag))
                         continue;
 
-                if (!strextend_with_separator(&s, ",", device_found_map[i].name, NULL))
+                if (!strextend_with_separator(&s, ",", device_found_map[i].name))
                         return -ENOMEM;
         }
 
index 6f60f09c4e7d51c4d2b0e48f644019438baa6a38..d9fcfc21a9caadf8d89851de25e78d0ce1013d97 100644 (file)
@@ -87,7 +87,7 @@ static void log_children_no_yet_killed(Set *pids) {
                 if (get_process_comm(PTR_TO_PID(p), &s) < 0)
                         (void) asprintf(&s, PID_FMT, PTR_TO_PID(p));
 
-                if (!strextend(&lst_child, ", ", s, NULL)) {
+                if (!strextend(&lst_child, ", ", s)) {
                         log_oom();
                         return;
                 }
index 15f5892228733edf753660fceb512893b86338b8..2318b65b0eda4d03ce5203d8a2bbb5567f003fe7 100644 (file)
@@ -843,7 +843,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
                 if (proc_cmdline_value_missing(key, value))
                         return 0;
 
-                if (!strextend_with_separator(&arg_root_options, ",", value, NULL))
+                if (!strextend_with_separator(&arg_root_options, ",", value))
                         return log_oom();
 
         } else if (streq(key, "roothash")) {
@@ -875,7 +875,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
                 if (proc_cmdline_value_missing(key, value))
                         return 0;
 
-                if (!strextend_with_separator(&arg_usr_options, ",", value, NULL))
+                if (!strextend_with_separator(&arg_usr_options, ",", value))
                         return log_oom();
 
         } else if (streq(key, "rw") && !value)
index 04a28c90537bd73ddab22084abbc672fd9a75264..b1e5452bb0a92e2bcc5e1ca2a1f71df93bd22cc4 100644 (file)
@@ -45,7 +45,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
                 if (proc_cmdline_value_missing(key, value))
                         return 0;
 
-                if (!strextend_with_separator(&arg_resume_options, ",", value, NULL))
+                if (!strextend_with_separator(&arg_resume_options, ",", value))
                         return log_oom();
 
         } else if (streq(key, "rootflags")) {
@@ -53,7 +53,7 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
                 if (proc_cmdline_value_missing(key, value))
                         return 0;
 
-                if (!strextend_with_separator(&arg_root_options, ",", value, NULL))
+                if (!strextend_with_separator(&arg_root_options, ",", value))
                         return log_oom();
 
         } else if (streq(key, "noresume")) {
index 86ff5bdfa27b441b86828b81cc59d0f5eb1e8ee2..99b1704aeec8a18492791ca330cc458ac4d3a0f5 100644 (file)
@@ -1471,7 +1471,7 @@ int message_append_basic(sd_bus_message *m, char type, const void *p, const void
                 if (c->enclosing != 0)
                         return -ENXIO;
 
-                e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
+                e = strextend(&c->signature, CHAR_TO_STR(type));
                 if (!e) {
                         m->poisoned = true;
                         return -ENOMEM;
@@ -1664,7 +1664,7 @@ _public_ int sd_bus_message_append_string_space(
                 if (c->enclosing != 0)
                         return -ENXIO;
 
-                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
+                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING));
                 if (!e) {
                         m->poisoned = true;
                         return -ENOMEM;
@@ -1768,7 +1768,7 @@ static int bus_message_open_array(
 
                 /* Extend the existing signature */
 
-                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
+                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents);
                 if (!e) {
                         m->poisoned = true;
                         return -ENOMEM;
@@ -1853,7 +1853,7 @@ static int bus_message_open_variant(
                 if (c->enclosing != 0)
                         return -ENXIO;
 
-                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
+                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT));
                 if (!e) {
                         m->poisoned = true;
                         return -ENOMEM;
@@ -1921,7 +1921,7 @@ static int bus_message_open_struct(
                 if (c->enclosing != 0)
                         return -ENXIO;
 
-                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
+                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END));
                 if (!e) {
                         m->poisoned = true;
                         return -ENOMEM;
@@ -2776,7 +2776,7 @@ _public_ int sd_bus_message_append_string_memfd(
                 if (c->enclosing != 0)
                         return -ENXIO;
 
-                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
+                e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING));
                 if (!e) {
                         m->poisoned = true;
                         return -ENOMEM;
index 3c839455b699943c2d2ffec1cc561d0087f46c63..6eb6cbb35ad4958f4d9ec7d74b997b4db49b859b 100644 (file)
@@ -206,7 +206,10 @@ static int call_get_addresses(
                 else
                         strcpy(buf_ifi, "");
 
-                if (!strextend(&addresses, prefix, inet_ntop(family, a, buffer, sizeof(buffer)), buf_ifi, NULL))
+                if (!strextend(&addresses,
+                               prefix,
+                               inet_ntop(family, a, buffer, sizeof(buffer)),
+                               buf_ifi))
                         return log_oom();
 
                 r = sd_bus_message_exit_container(reply);
index ed7eac029119286166520c18218187ac38ca34e7..a96a944ad1ae0f0ced697ddc2828e4cd6c3f6b5f 100644 (file)
@@ -710,9 +710,7 @@ static int install_chroot_dropin(
                                IN_SET(type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) ? "RootDirectory=" : "RootImage=", image_path, "\n"
                                "Environment=PORTABLE=", basename(image_path), "\n"
                                "BindReadOnlyPaths=", os_release_source, ":/run/host/os-release\n"
-                               "LogExtraFields=PORTABLE=", basename(image_path), "\n",
-                               NULL))
-
+                               "LogExtraFields=PORTABLE=", basename(image_path), "\n"))
                         return -ENOMEM;
         }
 
index d27d46b11f40f7d3efb6dbc16794f87fab179888..e435fd3a801ef571566d171dbb1dbc1ee76b9533 100644 (file)
@@ -290,7 +290,7 @@ static int bus_link_method_set_dns_servers_internal(sd_bus_message *message, voi
                         goto finalize;
                 }
 
-                if (!strextend_with_separator(&j, ", ", s, NULL)) {
+                if (!strextend_with_separator(&j, ", ", s)) {
                         r = -ENOMEM;
                         goto finalize;
                 }
@@ -387,7 +387,7 @@ int bus_link_method_set_domains(sd_bus_message *message, void *userdata, sd_bus_
                         name = prefixed;
                 }
 
-                if (!strextend_with_separator(&j, ", ", name, NULL))
+                if (!strextend_with_separator(&j, ", ", name))
                         return -ENOMEM;
         }
 
@@ -702,7 +702,7 @@ int bus_link_method_set_dnssec_negative_trust_anchors(sd_bus_message *message, v
                 if (r < 0)
                         return r;
 
-                if (!strextend_with_separator(&j, ", ", *i, NULL))
+                if (!strextend_with_separator(&j, ", ", *i))
                         return -ENOMEM;
         }
 
index 2bab2299fbe2b58c3db6fb8f8f726dc02c7b5f3a..bc17b6e1fbeabdc84501a5ddf051b7715e57da94 100644 (file)
@@ -1159,7 +1159,7 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
                 if (r < 0)
                         return log_error_errno(r, "Failed to unescape text '%s': %m", eq);
 
-                if (!strextend(&unescaped, "\n", NULL))
+                if (!strextend(&unescaped, "\n"))
                         return log_oom();
 
                 /* Note that we don't expand specifiers here, but that should be OK, as this is a programmatic
index e8b3dc78f910d5e0a1811f89a79d6d552824e9b3..0a1f2d67d43a66ea349cb1d984e500c48f69f612 100644 (file)
@@ -343,7 +343,7 @@ int config_parse(
                                 return -ENOBUFS;
                         }
 
-                        if (!strextend(&continuation, l, NULL)) {
+                        if (!strextend(&continuation, l)) {
                                 if (flags & CONFIG_PARSE_WARN)
                                         log_oom();
                                 return -ENOMEM;
index b1d7d689ea82fee1ca9e443802b7721d3c59a07b..bfde5cd5a0122b9fbc6a57e440b64fc6f30cfcae 100644 (file)
@@ -1340,12 +1340,12 @@ static int mount_partition(
                 if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0)
                         return -ENOMEM;
 
-                if (!strextend_with_separator(&options, ",", uid_option, NULL))
+                if (!strextend_with_separator(&options, ",", uid_option))
                         return -ENOMEM;
         }
 
         if (!isempty(m->mount_options))
-                if (!strextend_with_separator(&options, ",", m->mount_options, NULL))
+                if (!strextend_with_separator(&options, ",", m->mount_options))
                         return -ENOMEM;
 
         if (FLAGS_SET(flags, DISSECT_IMAGE_MKDIR)) {
index b19b3849aaa68b6cf378a067d12b202c197eb723..a6480b93a4e23ff60ca74ea8d0584a2ca046e3ab 100644 (file)
@@ -733,7 +733,7 @@ int mount_option_mangle(
                 }
 
                 /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */
-                if (!ent->name && !strextend_with_separator(&ret, ",", word, NULL))
+                if (!ent->name && !strextend_with_separator(&ret, ",", word))
                         return -ENOMEM;
         }
 
index 2845041cf49c056d4b93c1883eacf6580b64a4d3..d1c2168dea2917491bc5a758756d9babcd87e4fc 100644 (file)
@@ -61,7 +61,7 @@ int namespace_flags_to_string(unsigned long flags, char **ret) {
                 if ((flags & namespace_flag_map[i].flag) != namespace_flag_map[i].flag)
                         continue;
 
-                if (!strextend_with_separator(&s, " ", namespace_flag_map[i].name, NULL))
+                if (!strextend_with_separator(&s, " ", namespace_flag_map[i].name))
                         return -ENOMEM;
         }
 
index 3a72a13e1ab786683e917b678a940f88360706a8..906756292b1f16ad0b18200eee5eeed622957100 100644 (file)
@@ -96,13 +96,9 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
                  * Even if there are duplicates later in mount_option_mangle()
                  * they shouldn't hurt anyways as they override each other.
                  */
-                if (!strextend_with_separator(&options, ",",
-                                              mnt_fs_get_vfs_options(fs),
-                                              NULL))
+                if (!strextend_with_separator(&options, ",", mnt_fs_get_vfs_options(fs)))
                         return log_oom();
-                if (!strextend_with_separator(&options, ",",
-                                              mnt_fs_get_fs_options(fs),
-                                              NULL))
+                if (!strextend_with_separator(&options, ",", mnt_fs_get_fs_options(fs)))
                         return log_oom();
 
                 /* Ignore mount points we can't unmount because they
index d5efecbe65e70509848ca82afca5ad96d7c0851f..db3a3bd40aa9fc8667ab91c06c1588192d2d8299 100644 (file)
@@ -1476,7 +1476,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                 if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0)
                                         continue;
 
-                                if (!strextend_with_separator(&addresses, " ", str, NULL))
+                                if (!strextend_with_separator(&addresses, " ", str))
                                         return log_oom();
                         }
 
@@ -1513,7 +1513,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                              rbind == MS_REC ? ":rbind" : "") < 0)
                                         return log_oom();
 
-                                if (!strextend_with_separator(&paths, " ", str, NULL))
+                                if (!strextend_with_separator(&paths, " ", str))
                                         return log_oom();
                         }
                         if (r < 0)
@@ -1545,7 +1545,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                 if (asprintf(&str, "%s%s%s", target, isempty(option) ? "" : ":", strempty(option)) < 0)
                                         return log_oom();
 
-                                if (!strextend_with_separator(&paths, " ", str, NULL))
+                                if (!strextend_with_separator(&paths, " ", str))
                                         return log_oom();
                         }
                         if (r < 0)
@@ -1593,7 +1593,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                 if (!utf8_is_valid(str))
                                         continue;
 
-                                if (!strextend_with_separator(&fields, " ", str, NULL))
+                                if (!strextend_with_separator(&fields, " ", str))
                                         return log_oom();
                         }
                         if (r < 0)
@@ -1670,7 +1670,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                 if (r < 0)
                                         return r;
 
-                                if (!strextend_with_separator(&paths, " ", str, NULL))
+                                if (!strextend_with_separator(&paths, " ", str))
                                         return log_oom();
 
                                 r = sd_bus_message_exit_container(m);
index b74eb180f3fbeaffb29cfb4f00eb144176d11488..67f083ff93acb166f086ec51d57f5e747aaf664c 100644 (file)
@@ -244,9 +244,9 @@ static void test_strextend(void) {
 
         assert_se(strextend(&str, NULL));
         assert_se(streq_ptr(str, ""));
-        assert_se(strextend(&str, "", "0", "", "", "123", NULL));
+        assert_se(strextend(&str, "", "0", "", "", "123"));
         assert_se(streq_ptr(str, "0123"));
-        assert_se(strextend(&str, "456", "78", "9", NULL));
+        assert_se(strextend(&str, "456", "78", "9"));
         assert_se(streq_ptr(str, "0123456789"));
 }
 
@@ -265,13 +265,13 @@ static void test_strextend_with_separator(void) {
         assert_se(streq_ptr(str, ""));
         str = mfree(str);
 
-        assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc", NULL));
+        assert_se(strextend_with_separator(&str, "xyz", "a", "bb", "ccc"));
         assert_se(streq_ptr(str, "axyzbbxyzccc"));
         str = mfree(str);
 
-        assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234", NULL));
+        assert_se(strextend_with_separator(&str, ",", "start", "", "1", "234"));
         assert_se(streq_ptr(str, "start,,1,234"));
-        assert_se(strextend_with_separator(&str, ";", "more", "5", "678", NULL));
+        assert_se(strextend_with_separator(&str, ";", "more", "5", "678"));
         assert_se(streq_ptr(str, "start,,1,234;more;5;678"));
 }
 
index 6a1215d6263957eb85c4dc1fe90a7811011b23de..4ebdaddb5b182dff0b179ed236044031d5bf3b82 100644 (file)
@@ -3387,7 +3387,7 @@ static int run(int argc, char *argv[]) {
                         if (!j)
                                 return log_oom();
 
-                        if (!strextend(&t, "\n\t", j, NULL))
+                        if (!strextend(&t, "\n\t", j))
                                 return log_oom();
                 }
 
index 56d680c12fc2fc192e27356533ed76f93c933780..06a97d86713ee8781ab2c25cb7cd62bcda87dd21 100644 (file)
@@ -1241,7 +1241,7 @@ int udev_rules_parse_file(UdevRules *rules, const char *filename) {
                         if (strlen(continuation) + len >= UDEV_LINE_SIZE)
                                 ignore_line = true;
 
-                        if (!strextend(&continuation, line, NULL))
+                        if (!strextend(&continuation, line))
                                 return log_oom();
 
                         if (!ignore_line) {