Merge pull request #18150 from poettering/strextend-tweak

tree-wide: beef up strextend() a bit
This commit is contained in:
Lennart Poettering 2021-01-06 19:55:29 +01:00 committed by GitHub
commit 8b1ac00fa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) if (!escaped)
return -ENOMEM; return -ENOMEM;
if (!strextend(&s, escaped, "/", NULL)) if (!strextend(&s, escaped, "/"))
return -ENOMEM; return -ENOMEM;
dash = strchr(dash+1, '-'); dash = strchr(dash+1, '-');
@ -1629,7 +1629,7 @@ int cg_slice_to_path(const char *unit, char **ret) {
if (!e) if (!e)
return -ENOMEM; return -ENOMEM;
if (!strextend(&s, e, NULL)) if (!strextend(&s, e))
return -ENOMEM; return -ENOMEM;
*ret = TAKE_PTR(s); *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 */ /* Preserve the trailing slash */
if (flags & CHASE_TRAIL_SLASH) if (flags & CHASE_TRAIL_SLASH)
if (!strextend(&done, "/", NULL)) if (!strextend(&done, "/"))
return -ENOMEM; return -ENOMEM;
break; break;
@ -1005,7 +1005,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
if (streq_ptr(done, "/")) if (streq_ptr(done, "/"))
*done = '\0'; *done = '\0';
if (!strextend(&done, first, todo, NULL)) if (!strextend(&done, first, todo))
return -ENOMEM; return -ENOMEM;
exists = false; exists = false;
@ -1098,7 +1098,7 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
if (streq(done, "/")) if (streq(done, "/"))
*done = '\0'; *done = '\0';
if (!strextend(&done, first, NULL)) if (!strextend(&done, first))
return -ENOMEM; return -ENOMEM;
} }

View File

@ -791,10 +791,10 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
return *ibuf; return *ibuf;
} }
char *strextend_with_separator(char **x, const char *separator, ...) { char *strextend_with_separator_internal(char **x, const char *separator, ...) {
bool need_separator;
size_t f, l, l_separator; size_t f, l, l_separator;
char *r, *p; bool need_separator;
char *nr, *p;
va_list ap; va_list ap;
assert(x); assert(x);
@ -818,7 +818,7 @@ char *strextend_with_separator(char **x, const char *separator, ...) {
if (need_separator) if (need_separator)
n += l_separator; n += l_separator;
if (n > ((size_t) -1) - l) { if (n >= SIZE_MAX - l) {
va_end(ap); va_end(ap);
return NULL; return NULL;
} }
@ -830,11 +830,12 @@ char *strextend_with_separator(char **x, const char *separator, ...) {
need_separator = !isempty(*x); need_separator = !isempty(*x);
r = realloc(*x, l+1); nr = realloc(*x, GREEDY_ALLOC_ROUND_UP(l+1));
if (!r) if (!nr)
return NULL; return NULL;
p = r + f; *x = nr;
p = nr + f;
va_start(ap, separator); va_start(ap, separator);
for (;;) { for (;;) {
@ -853,12 +854,11 @@ char *strextend_with_separator(char **x, const char *separator, ...) {
} }
va_end(ap); va_end(ap);
assert(p == r + l); assert(p == nr + l);
*p = 0; *p = 0;
*x = r;
return r + l; return p;
} }
char *strrep(const char *s, unsigned n) { 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 *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); 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_type) {
if (!argument_direction || streq(argument_direction, "in")) { 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(); return log_oom();
} else if (streq(argument_direction, "out")) { } else if (streq(argument_direction, "out")) {
if (!strextend(&context->member_result, argument_type, NULL)) if (!strextend(&context->member_result, argument_type))
return log_oom(); return log_oom();
} else } else
log_error("Unexpected method <arg> direction value '%s'.", argument_direction); 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_type) {
if (!argument_direction || streq(argument_direction, "out")) { 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(); return log_oom();
} else } else
log_error("Unexpected signal <arg> direction value '%s'.", argument_direction); 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)) if (!FLAGS_SET(flags, device_found_map[i].flag))
continue; continue;
if (!strextend_with_separator(&s, ",", device_found_map[i].name, NULL)) if (!strextend_with_separator(&s, ",", device_found_map[i].name))
return -ENOMEM; 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) if (get_process_comm(PTR_TO_PID(p), &s) < 0)
(void) asprintf(&s, PID_FMT, PTR_TO_PID(p)); (void) asprintf(&s, PID_FMT, PTR_TO_PID(p));
if (!strextend(&lst_child, ", ", s, NULL)) { if (!strextend(&lst_child, ", ", s)) {
log_oom(); log_oom();
return; 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)) if (proc_cmdline_value_missing(key, value))
return 0; return 0;
if (!strextend_with_separator(&arg_root_options, ",", value, NULL)) if (!strextend_with_separator(&arg_root_options, ",", value))
return log_oom(); return log_oom();
} else if (streq(key, "roothash")) { } 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)) if (proc_cmdline_value_missing(key, value))
return 0; return 0;
if (!strextend_with_separator(&arg_usr_options, ",", value, NULL)) if (!strextend_with_separator(&arg_usr_options, ",", value))
return log_oom(); return log_oom();
} else if (streq(key, "rw") && !value) } 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)) if (proc_cmdline_value_missing(key, value))
return 0; return 0;
if (!strextend_with_separator(&arg_resume_options, ",", value, NULL)) if (!strextend_with_separator(&arg_resume_options, ",", value))
return log_oom(); return log_oom();
} else if (streq(key, "rootflags")) { } 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)) if (proc_cmdline_value_missing(key, value))
return 0; return 0;
if (!strextend_with_separator(&arg_root_options, ",", value, NULL)) if (!strextend_with_separator(&arg_root_options, ",", value))
return log_oom(); return log_oom();
} else if (streq(key, "noresume")) { } 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) if (c->enclosing != 0)
return -ENXIO; return -ENXIO;
e = strextend(&c->signature, CHAR_TO_STR(type), NULL); e = strextend(&c->signature, CHAR_TO_STR(type));
if (!e) { if (!e) {
m->poisoned = true; m->poisoned = true;
return -ENOMEM; return -ENOMEM;
@ -1664,7 +1664,7 @@ _public_ int sd_bus_message_append_string_space(
if (c->enclosing != 0) if (c->enclosing != 0)
return -ENXIO; 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) { if (!e) {
m->poisoned = true; m->poisoned = true;
return -ENOMEM; return -ENOMEM;
@ -1768,7 +1768,7 @@ static int bus_message_open_array(
/* Extend the existing signature */ /* 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) { if (!e) {
m->poisoned = true; m->poisoned = true;
return -ENOMEM; return -ENOMEM;
@ -1853,7 +1853,7 @@ static int bus_message_open_variant(
if (c->enclosing != 0) if (c->enclosing != 0)
return -ENXIO; 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) { if (!e) {
m->poisoned = true; m->poisoned = true;
return -ENOMEM; return -ENOMEM;
@ -1921,7 +1921,7 @@ static int bus_message_open_struct(
if (c->enclosing != 0) if (c->enclosing != 0)
return -ENXIO; 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) { if (!e) {
m->poisoned = true; m->poisoned = true;
return -ENOMEM; return -ENOMEM;
@ -2776,7 +2776,7 @@ _public_ int sd_bus_message_append_string_memfd(
if (c->enclosing != 0) if (c->enclosing != 0)
return -ENXIO; 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) { if (!e) {
m->poisoned = true; m->poisoned = true;
return -ENOMEM; return -ENOMEM;

View File

@ -206,7 +206,10 @@ static int call_get_addresses(
else else
strcpy(buf_ifi, ""); 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(); return log_oom();
r = sd_bus_message_exit_container(reply); 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" IN_SET(type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME) ? "RootDirectory=" : "RootImage=", image_path, "\n"
"Environment=PORTABLE=", basename(image_path), "\n" "Environment=PORTABLE=", basename(image_path), "\n"
"BindReadOnlyPaths=", os_release_source, ":/run/host/os-release\n" "BindReadOnlyPaths=", os_release_source, ":/run/host/os-release\n"
"LogExtraFields=PORTABLE=", basename(image_path), "\n", "LogExtraFields=PORTABLE=", basename(image_path), "\n"))
NULL))
return -ENOMEM; return -ENOMEM;
} }

View File

@ -290,7 +290,7 @@ static int bus_link_method_set_dns_servers_internal(sd_bus_message *message, voi
goto finalize; goto finalize;
} }
if (!strextend_with_separator(&j, ", ", s, NULL)) { if (!strextend_with_separator(&j, ", ", s)) {
r = -ENOMEM; r = -ENOMEM;
goto finalize; goto finalize;
} }
@ -387,7 +387,7 @@ int bus_link_method_set_domains(sd_bus_message *message, void *userdata, sd_bus_
name = prefixed; name = prefixed;
} }
if (!strextend_with_separator(&j, ", ", name, NULL)) if (!strextend_with_separator(&j, ", ", name))
return -ENOMEM; return -ENOMEM;
} }
@ -702,7 +702,7 @@ int bus_link_method_set_dnssec_negative_trust_anchors(sd_bus_message *message, v
if (r < 0) if (r < 0)
return r; return r;
if (!strextend_with_separator(&j, ", ", *i, NULL)) if (!strextend_with_separator(&j, ", ", *i))
return -ENOMEM; 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) if (r < 0)
return log_error_errno(r, "Failed to unescape text '%s': %m", eq); return log_error_errno(r, "Failed to unescape text '%s': %m", eq);
if (!strextend(&unescaped, "\n", NULL)) if (!strextend(&unescaped, "\n"))
return log_oom(); return log_oom();
/* Note that we don't expand specifiers here, but that should be OK, as this is a programmatic /* 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; return -ENOBUFS;
} }
if (!strextend(&continuation, l, NULL)) { if (!strextend(&continuation, l)) {
if (flags & CONFIG_PARSE_WARN) if (flags & CONFIG_PARSE_WARN)
log_oom(); log_oom();
return -ENOMEM; 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) if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0)
return -ENOMEM; return -ENOMEM;
if (!strextend_with_separator(&options, ",", uid_option, NULL)) if (!strextend_with_separator(&options, ",", uid_option))
return -ENOMEM; return -ENOMEM;
} }
if (!isempty(m->mount_options)) if (!isempty(m->mount_options))
if (!strextend_with_separator(&options, ",", m->mount_options, NULL)) if (!strextend_with_separator(&options, ",", m->mount_options))
return -ENOMEM; return -ENOMEM;
if (FLAGS_SET(flags, DISSECT_IMAGE_MKDIR)) { 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 '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; 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) if ((flags & namespace_flag_map[i].flag) != namespace_flag_map[i].flag)
continue; continue;
if (!strextend_with_separator(&s, " ", namespace_flag_map[i].name, NULL)) if (!strextend_with_separator(&s, " ", namespace_flag_map[i].name))
return -ENOMEM; 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() * Even if there are duplicates later in mount_option_mangle()
* they shouldn't hurt anyways as they override each other. * they shouldn't hurt anyways as they override each other.
*/ */
if (!strextend_with_separator(&options, ",", if (!strextend_with_separator(&options, ",", mnt_fs_get_vfs_options(fs)))
mnt_fs_get_vfs_options(fs),
NULL))
return log_oom(); return log_oom();
if (!strextend_with_separator(&options, ",", if (!strextend_with_separator(&options, ",", mnt_fs_get_fs_options(fs)))
mnt_fs_get_fs_options(fs),
NULL))
return log_oom(); return log_oom();
/* Ignore mount points we can't unmount because they /* 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) if (in_addr_prefix_to_string(family, (union in_addr_union *) ap, prefixlen, &str) < 0)
continue; continue;
if (!strextend_with_separator(&addresses, " ", str, NULL)) if (!strextend_with_separator(&addresses, " ", str))
return log_oom(); 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) rbind == MS_REC ? ":rbind" : "") < 0)
return log_oom(); return log_oom();
if (!strextend_with_separator(&paths, " ", str, NULL)) if (!strextend_with_separator(&paths, " ", str))
return log_oom(); return log_oom();
} }
if (r < 0) 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) if (asprintf(&str, "%s%s%s", target, isempty(option) ? "" : ":", strempty(option)) < 0)
return log_oom(); return log_oom();
if (!strextend_with_separator(&paths, " ", str, NULL)) if (!strextend_with_separator(&paths, " ", str))
return log_oom(); return log_oom();
} }
if (r < 0) 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)) if (!utf8_is_valid(str))
continue; continue;
if (!strextend_with_separator(&fields, " ", str, NULL)) if (!strextend_with_separator(&fields, " ", str))
return log_oom(); return log_oom();
} }
if (r < 0) if (r < 0)
@ -1670,7 +1670,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
if (r < 0) if (r < 0)
return r; return r;
if (!strextend_with_separator(&paths, " ", str, NULL)) if (!strextend_with_separator(&paths, " ", str))
return log_oom(); return log_oom();
r = sd_bus_message_exit_container(m); 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(strextend(&str, NULL));
assert_se(streq_ptr(str, "")); 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(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")); assert_se(streq_ptr(str, "0123456789"));
} }
@ -265,13 +265,13 @@ static void test_strextend_with_separator(void) {
assert_se(streq_ptr(str, "")); assert_se(streq_ptr(str, ""));
str = mfree(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")); assert_se(streq_ptr(str, "axyzbbxyzccc"));
str = mfree(str); 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(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")); 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) if (!j)
return log_oom(); return log_oom();
if (!strextend(&t, "\n\t", j, NULL)) if (!strextend(&t, "\n\t", j))
return log_oom(); 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) if (strlen(continuation) + len >= UDEV_LINE_SIZE)
ignore_line = true; ignore_line = true;
if (!strextend(&continuation, line, NULL)) if (!strextend(&continuation, line))
return log_oom(); return log_oom();
if (!ignore_line) { if (!ignore_line) {