string-util: imply NULL termination of strextend() argument list

The trailing NULL in the argument list is now implied (similar to
what we already have in place in strjoin()).
This commit is contained in:
Lennart Poettering 2021-01-05 15:03:41 +01:00
parent 0ec1dc5ba3
commit c2bc710b24
23 changed files with 61 additions and 63 deletions

View 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);

View 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;
}

View 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) {

View 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);

View 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);

View 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;
}

View 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;
}

View 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)

View 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")) {

View 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;

View 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);

View 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;
}

View 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;
}

View 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

View 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;

View 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)) {

View 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;
}

View 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;
}

View 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

View 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);

View 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"));
}

View 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();
}

View 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) {