diff options
Diffstat (limited to 'src')
48 files changed, 509 insertions, 298 deletions
diff --git a/src/basic/bitmap.c b/src/basic/bitmap.c index bf9d8d4d7c..7ea3357031 100644 --- a/src/basic/bitmap.c +++ b/src/basic/bitmap.c @@ -145,7 +145,10 @@ bool bitmap_isclear(Bitmap *b) { void bitmap_clear(Bitmap *b) { assert(b); + free(b->bitmaps); + b->bitmaps = NULL; b->n_bitmaps = 0; + b->bitmaps_allocated = 0; } bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) { @@ -184,6 +187,9 @@ bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) { } bool bitmap_equal(Bitmap *a, Bitmap *b) { + size_t common_n_bitmaps; + Bitmap *c; + unsigned i; if (!a ^ !b) return false; @@ -191,8 +197,14 @@ bool bitmap_equal(Bitmap *a, Bitmap *b) { if (!a) return true; - if (a->n_bitmaps != b->n_bitmaps) + common_n_bitmaps = MIN(a->n_bitmaps, b->n_bitmaps); + if (memcmp(a->bitmaps, b->bitmaps, sizeof(uint64_t) * common_n_bitmaps) != 0) return false; - return memcmp(a->bitmaps, b->bitmaps, sizeof(uint64_t) * a->n_bitmaps) == 0; + c = a->n_bitmaps > b->n_bitmaps ? a : b; + for (i = common_n_bitmaps; i < c->n_bitmaps; i++) + if (c->bitmaps[i] != 0) + return false; + + return true; } diff --git a/src/basic/copy.c b/src/basic/copy.c index e2d356d676..33427c6a73 100644 --- a/src/basic/copy.c +++ b/src/basic/copy.c @@ -467,8 +467,7 @@ int copy_xattr(int fdf, int fdt) { sza *= 2; - free(bufa); - bufa = NULL; + bufa = mfree(bufa); } p = bufa; @@ -491,8 +490,7 @@ int copy_xattr(int fdf, int fdt) { if (m < 0) { if (errno == ERANGE) { szb *= 2; - free(bufb); - bufb = NULL; + bufb = mfree(bufb); continue; } diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 62f99b322e..8fd3149276 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -583,7 +583,7 @@ int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool translate_ } else { p = strndup(sa->un.sun_path, sizeof(sa->un.sun_path)); - if (!ret) + if (!p) return -ENOMEM; } diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c index bf52463d81..fa530da456 100644 --- a/src/basic/unit-name.c +++ b/src/basic/unit-name.c @@ -673,6 +673,7 @@ int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, c int slice_build_parent_slice(const char *slice, char **ret) { char *s, *dash; + int r; assert(slice); assert(ret); @@ -693,11 +694,11 @@ int slice_build_parent_slice(const char *slice, char **ret) { if (dash) strcpy(dash, ".slice"); else { - free(s); - - s = strdup("-.slice"); - if (!s) - return -ENOMEM; + r = free_and_strdup(&s, "-.slice"); + if (r < 0) { + free(s); + return r; + } } *ret = s; diff --git a/src/basic/util.c b/src/basic/util.c index 1c15fbc172..a968e2156d 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -115,17 +115,23 @@ size_t page_size(void) { return pgsz; } -bool streq_ptr(const char *a, const char *b) { - - /* Like streq(), but tries to make sense of NULL pointers */ +int strcmp_ptr(const char *a, const char *b) { + /* Like strcmp(), but tries to make sense of NULL pointers */ if (a && b) - return streq(a, b); + return strcmp(a, b); - if (!a && !b) - return true; + if (!a && b) + return -1; - return false; + if (a && !b) + return 1; + + return 0; +} + +bool streq_ptr(const char *a, const char *b) { + return strcmp_ptr(a, b) == 0; } char* endswith(const char *s, const char *postfix) { diff --git a/src/basic/util.h b/src/basic/util.h index c2e5cc610b..88c44273d4 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -71,6 +71,7 @@ size_t page_size(void) _pure_; #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0) bool streq_ptr(const char *a, const char *b) _pure_; +int strcmp_ptr(const char *a, const char *b) _pure_; #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n))) @@ -84,6 +85,11 @@ bool streq_ptr(const char *a, const char *b) _pure_; #define malloc0(n) (calloc((n), 1)) +static inline void *mfree(void *memory) { + free(memory); + return NULL; +} + static inline const char* yes_no(bool b) { return b ? "yes" : "no"; } diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index dde0c41744..4ac193e22a 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -1140,13 +1140,11 @@ static VOID config_entry_add_from_file(Config *config, EFI_HANDLE *device, CHAR1 config_add_entry(config, entry); } -static VOID config_load(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path) { - EFI_FILE_HANDLE entries_dir; - EFI_STATUS err; +static VOID config_load_defaults(Config *config, EFI_FILE *root_dir) { CHAR8 *content = NULL; UINTN sec; UINTN len; - UINTN i; + EFI_STATUS err; len = file_read(root_dir, L"\\loader\\loader.conf", 0, 0, &content); if (len > 0) @@ -1159,6 +1157,11 @@ static VOID config_load(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, config->timeout_sec = sec; } else config->timeout_sec_efivar = -1; +} + +static VOID config_load_entries(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, CHAR16 *loaded_image_path) { + EFI_FILE_HANDLE entries_dir; + EFI_STATUS err; err = uefi_call_wrapper(root_dir->Open, 5, root_dir, &entries_dir, L"\\loader\\entries", EFI_FILE_MODE_READ, 0ULL); if (!EFI_ERROR(err)) { @@ -1195,8 +1198,11 @@ static VOID config_load(Config *config, EFI_HANDLE *device, EFI_FILE *root_dir, } uefi_call_wrapper(entries_dir->Close, 1, entries_dir); } +} + +static VOID config_sort_entries(Config *config) { + UINTN i; - /* sort entries after version number */ for (i = 1; i < config->entry_count; i++) { BOOLEAN more; UINTN k; @@ -1738,12 +1744,19 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *sys_table) { loaded_image_path = DevicePathToStr(loaded_image->FilePath); efivar_set(L"LoaderImageIdentifier", loaded_image_path, FALSE); - /* scan "\loader\entries\*.conf" files */ ZeroMem(&config, sizeof(Config)); - config_load(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path); + config_load_defaults(&config, root_dir); - /* if we find some well-known loaders, add them to the end of the list */ + /* scan /EFI/Linux/ directory */ config_entry_add_linux(&config, loaded_image, root_dir); + + /* scan /loader/entries/\*.conf files */ + config_load_entries(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path); + + /* sort entries after version number */ + config_sort_entries(&config); + + /* if we find some well-known loaders, add them to the end of the list */ config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path, L"auto-windows", 'w', L"Windows Boot Manager", L"\\EFI\\Microsoft\\Boot\\bootmgfw.efi"); config_entry_add_loader_auto(&config, loaded_image->DeviceHandle, root_dir, loaded_image_path, diff --git a/src/bus-proxyd/bus-xml-policy.c b/src/bus-proxyd/bus-xml-policy.c index dab5acbcb4..9a3b451c56 100644 --- a/src/bus-proxyd/bus-xml-policy.c +++ b/src/bus-proxyd/bus-xml-policy.c @@ -586,10 +586,8 @@ static int file_load(Policy *p, const char *path) { case POLICY_ITEM_SEND: case POLICY_ITEM_RECV: - if (streq(name, "*")) { - free(name); - name = NULL; - } + if (streq(name, "*")) + name = mfree(name); break; diff --git a/src/bus-proxyd/driver.c b/src/bus-proxyd/driver.c index 1cb5ea5008..951f515808 100644 --- a/src/bus-proxyd/driver.c +++ b/src/bus-proxyd/driver.c @@ -71,6 +71,27 @@ static int get_creds_by_message(sd_bus *bus, sd_bus_message *m, uint64_t mask, s return get_creds_by_name(bus, name, mask, _creds, error); } +static int driver_activation(sd_bus_message *reply, void *userdata, sd_bus_error *error) { + _cleanup_bus_message_unref_ sd_bus_message *m = NULL; + ProxyActivation *activation = userdata; + + /* + * The org.freedesktop.DBus.Peer.Ping() call returned. We don't care + * whether this succeeded, failed, was not implemented or timed out. We + * cannot assume that the target reacts to this properly. Hence, just + * send the reply to the activation request and be done. + */ + + m = activation->request; /* claim reference */ + + --activation->proxy->n_activations; + LIST_REMOVE(activations_by_proxy, activation->proxy->activations, activation); + sd_bus_slot_unref(activation->slot); + free(activation); + + return synthetic_reply_method_return(m, "u", BUS_START_REPLY_SUCCESS); +} + int bus_proxy_process_driver(Proxy *p, sd_bus *a, sd_bus *b, sd_bus_message *m, SharedPolicy *sp, const struct ucred *ucred, Set *owned_names) { int r; @@ -441,27 +462,29 @@ int bus_proxy_process_driver(Proxy *p, sd_bus *a, sd_bus *b, sd_bus_message *m, name_list = (struct kdbus_info *) ((uint8_t *) a->kdbus_buffer + cmd.offset); KDBUS_FOREACH(name, name_list, cmd.list_size) { - const char *entry_name = NULL; struct kdbus_item *item; char *n; - KDBUS_ITEM_FOREACH(item, name, items) - if (item->type == KDBUS_ITEM_OWNED_NAME) - entry_name = item->name.name; - - if (!streq_ptr(entry_name, arg0)) - continue; - - if (asprintf(&n, ":1.%llu", (unsigned long long) name->id) < 0) { - err = -ENOMEM; - break; + KDBUS_ITEM_FOREACH(item, name, items) { + if (item->type == KDBUS_ITEM_OWNED_NAME) { + if (!streq_ptr(item->name.name, arg0)) + continue; + + if (asprintf(&n, ":1.%llu", (unsigned long long) name->id) < 0) { + err = -ENOMEM; + break; + } + + r = strv_consume(&owners, n); + if (r < 0) { + err = r; + break; + } + } } - r = strv_consume(&owners, n); - if (r < 0) { - err = r; + if (err < 0) break; - } } r = bus_kernel_cmd_free(a, cmd.offset); @@ -585,7 +608,9 @@ int bus_proxy_process_driver(Proxy *p, sd_bus *a, sd_bus *b, sd_bus_message *m, } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus", "StartServiceByName")) { _cleanup_bus_message_unref_ sd_bus_message *msg = NULL; + ProxyActivation *activation; const char *name; + uint64_t cookie; uint32_t flags; if (!sd_bus_message_has_signature(m, "su")) @@ -604,21 +629,46 @@ int bus_proxy_process_driver(Proxy *p, sd_bus *a, sd_bus *b, sd_bus_message *m, if (r != -ESRCH) return synthetic_reply_method_errno(m, r, NULL); - r = sd_bus_message_new_method_call( - a, - &msg, - name, - "/", - "org.freedesktop.DBus.Peer", - "Ping"); + if (p->n_activations >= PROXY_ACTIVATIONS_MAX) + return synthetic_reply_method_errno(m, -EMFILE, NULL); + + r = sd_bus_message_get_cookie(m, &cookie); + if (r < 0) + return synthetic_reply_method_errno(m, r, NULL); + + r = sd_bus_message_new_method_call(a, + &msg, + name, + "/", + "org.freedesktop.DBus.Peer", + "Ping"); if (r < 0) return synthetic_reply_method_errno(m, r, NULL); - r = sd_bus_send(a, msg, NULL); + r = bus_message_seal(msg, cookie, BUS_DEFAULT_TIMEOUT); if (r < 0) return synthetic_reply_method_errno(m, r, NULL); - return synthetic_reply_method_return(m, "u", BUS_START_REPLY_SUCCESS); + activation = new0(ProxyActivation, 1); + if (!activation) + return synthetic_reply_method_errno(m, -ENOMEM, NULL); + + r = sd_bus_call_async(a, + &activation->slot, + msg, + driver_activation, + activation, + 0); + if (r < 0) { + free(activation); + return synthetic_reply_method_errno(m, r, NULL); + } + + activation->proxy = p; + activation->request = sd_bus_message_ref(m); + LIST_PREPEND(activations_by_proxy, p->activations, activation); + ++p->n_activations; + return 1; } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus", "UpdateActivationEnvironment")) { _cleanup_bus_message_unref_ sd_bus_message *msg = NULL; @@ -657,8 +707,8 @@ int bus_proxy_process_driver(Proxy *p, sd_bus *a, sd_bus *b, sd_bus_message *m, if (r < 0) return synthetic_reply_method_errno(m, r, NULL); - if (!args) - return synthetic_reply_method_errno(m, -EINVAL, NULL); + if (strv_isempty(args)) /* nothing to do? */ + return synthetic_reply_method_return(m, NULL); r = sd_bus_message_new_method_call( a, diff --git a/src/bus-proxyd/proxy.c b/src/bus-proxyd/proxy.c index c37b09b9c0..df361ac400 100644 --- a/src/bus-proxyd/proxy.c +++ b/src/bus-proxyd/proxy.c @@ -261,9 +261,18 @@ int proxy_new(Proxy **out, int in_fd, int out_fd, const char *destination) { } Proxy *proxy_free(Proxy *p) { + ProxyActivation *activation; + if (!p) return NULL; + while ((activation = p->activations)) { + LIST_REMOVE(activations_by_proxy, p->activations, activation); + sd_bus_message_unref(activation->request); + sd_bus_slot_unref(activation->slot); + free(activation); + } + sd_bus_flush_close_unref(p->local_bus); sd_bus_flush_close_unref(p->destination_bus); set_free_free(p->owned_names); @@ -644,6 +653,10 @@ static int process_hello(Proxy *p, sd_bus_message *m) { if (r < 0) return log_error_errno(r, "Failed to append sender to NameAcquired message: %m"); + r = sd_bus_message_set_destination(n, p->destination_bus->unique_name); + if (r < 0) + return log_error_errno(r, "Failed to set destination for NameAcquired message: %m"); + r = bus_seal_synthetic_message(p->local_bus, n); if (r < 0) return log_error_errno(r, "Failed to seal NameAcquired message: %m"); diff --git a/src/bus-proxyd/proxy.h b/src/bus-proxyd/proxy.h index ccb951c109..6aac650ac9 100644 --- a/src/bus-proxyd/proxy.h +++ b/src/bus-proxyd/proxy.h @@ -25,6 +25,9 @@ #include "bus-xml-policy.h" typedef struct Proxy Proxy; +typedef struct ProxyActivation ProxyActivation; + +#define PROXY_ACTIVATIONS_MAX (16) /* max parallel activation requests */ struct Proxy { sd_bus *local_bus; @@ -37,12 +40,22 @@ struct Proxy { Set *owned_names; SharedPolicy *policy; + LIST_HEAD(ProxyActivation, activations); + size_t n_activations; + bool got_hello : 1; bool queue_overflow : 1; bool message_matched : 1; bool synthetic_matched : 1; }; +struct ProxyActivation { + LIST_FIELDS(ProxyActivation, activations_by_proxy); + Proxy *proxy; + sd_bus_message *request; + sd_bus_slot *slot; +}; + int proxy_new(Proxy **out, int in_fd, int out_fd, const char *dest); Proxy *proxy_free(Proxy *p); diff --git a/src/bus-proxyd/synthesize.c b/src/bus-proxyd/synthesize.c index 3ecedfd575..15d99103f6 100644 --- a/src/bus-proxyd/synthesize.c +++ b/src/bus-proxyd/synthesize.c @@ -214,22 +214,13 @@ int synthesize_name_acquired(Proxy *p, sd_bus *a, sd_bus *b, sd_bus_message *m) if (r < 0) return r; - r = bus_seal_synthetic_message(b, n); + r = sd_bus_message_set_destination(n, a->unique_name); if (r < 0) return r; - /* - * Make sure to only forward NameLost/NameAcquired messages if they - * match an installed MATCH rule of the local client. We really must - * not send messages the client doesn't expect. - */ - - r = bus_match_run(b, &b->match_callbacks, n); - if (r >= 0 && p->message_matched) - r = sd_bus_send(b, n, NULL); - - p->message_matched = false; - p->synthetic_matched = false; + r = bus_seal_synthetic_message(b, n); + if (r < 0) + return r; - return r; + return sd_bus_send(b, n, NULL); } diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index a48cb4029a..ba73cc410e 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -3508,9 +3508,7 @@ static int load_from_path(Unit *u, const char *path) { r = open_follow(&filename, &f, symlink_names, &id); if (r < 0) { - free(filename); - filename = NULL; - + filename = mfree(filename); if (r != -ENOENT) return r; } @@ -3534,9 +3532,7 @@ static int load_from_path(Unit *u, const char *path) { r = open_follow(&filename, &f, symlink_names, &id); if (r < 0) { - free(filename); - filename = NULL; - + filename = mfree(filename); if (r != -ENOENT) return r; diff --git a/src/core/main.c b/src/core/main.c index 6ae8b51544..a66fb18418 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1844,13 +1844,11 @@ finish: arg_default_rlimit[j] = NULL; } - free(arg_default_unit); - arg_default_unit = NULL; + arg_default_unit = mfree(arg_default_unit); free_join_controllers(); - strv_free(arg_default_environment); - arg_default_environment = NULL; + arg_default_environment = strv_free(arg_default_environment); set_free(arg_syscall_archs); arg_syscall_archs = NULL; diff --git a/src/core/snapshot.c b/src/core/snapshot.c index 1e634b9bc1..9518e21f36 100644 --- a/src/core/snapshot.c +++ b/src/core/snapshot.c @@ -217,8 +217,7 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, sd_bus_error *e, break; } - free(n); - n = NULL; + n = mfree(n); } } diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 5c6c7c0ed8..74fa90a233 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -333,8 +333,7 @@ static int get_password(const char *vol, const char *src, usec_t until, bool acc /* If the description string is simply the * volume name, then let's not show this * twice */ - free(description); - description = NULL; + description = mfree(description); } if (mount_point && description) diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 3805b29437..05f1ae2646 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -715,10 +715,8 @@ static int parse_argv(int argc, char *argv[]) { path_kill_slashes(arg_root); - if (path_equal(arg_root, "/")) { - free(arg_root); - arg_root = NULL; - } + if (path_equal(arg_root, "/")) + arg_root = mfree(arg_root); break; @@ -728,9 +726,8 @@ static int parse_argv(int argc, char *argv[]) { return -EINVAL; } - free(arg_locale); - arg_locale = strdup(optarg); - if (!arg_locale) + r = free_and_strdup(&arg_locale, optarg); + if (r < 0) return log_oom(); break; @@ -741,9 +738,8 @@ static int parse_argv(int argc, char *argv[]) { return -EINVAL; } - free(arg_locale_messages); - arg_locale_messages = strdup(optarg); - if (!arg_locale_messages) + r = free_and_strdup(&arg_locale_messages, optarg); + if (r < 0) return log_oom(); break; @@ -754,19 +750,16 @@ static int parse_argv(int argc, char *argv[]) { return -EINVAL; } - free(arg_timezone); - arg_timezone = strdup(optarg); - if (!arg_timezone) + r = free_and_strdup(&arg_timezone, optarg); + if (r < 0) return log_oom(); break; case ARG_ROOT_PASSWORD: - free(arg_root_password); - arg_root_password = strdup(optarg); - if (!arg_root_password) + r = free_and_strdup(&arg_root_password, optarg); + if (r < 0) return log_oom(); - break; case ARG_ROOT_PASSWORD_FILE: @@ -785,9 +778,8 @@ static int parse_argv(int argc, char *argv[]) { return -EINVAL; } - free(arg_hostname); - arg_hostname = strdup(optarg); - if (!arg_hostname) + r = free_and_strdup(&arg_hostname, optarg); + if (r < 0) return log_oom(); break; diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c index 9fc576fb45..50acb7595c 100644 --- a/src/gpt-auto-generator/gpt-auto-generator.c +++ b/src/gpt-auto-generator/gpt-auto-generator.c @@ -727,9 +727,8 @@ static int enumerate_partitions(dev_t devnum) { boot_nr = nr; - free(boot); - boot = strdup(subnode); - if (!boot) + r = free_and_strdup(&boot, subnode); + if (r < 0) return log_oom(); } else if (sd_id128_equal(type_id, GPT_HOME)) { @@ -741,9 +740,8 @@ static int enumerate_partitions(dev_t devnum) { home_nr = nr; home_rw = !(flags & GPT_FLAG_READ_ONLY), - free(home); - home = strdup(subnode); - if (!home) + r = free_and_strdup(&home, subnode); + if (r < 0) return log_oom(); } else if (sd_id128_equal(type_id, GPT_SRV)) { @@ -755,9 +753,8 @@ static int enumerate_partitions(dev_t devnum) { srv_nr = nr; srv_rw = !(flags & GPT_FLAG_READ_ONLY), - free(srv); - srv = strdup(subnode); - if (!srv) + r = free_and_strdup(&srv, subnode); + if (r < 0) return log_oom(); } } diff --git a/src/import/pull-dkr.c b/src/import/pull-dkr.c index 67ca1ce8e4..c918f43446 100644 --- a/src/import/pull-dkr.c +++ b/src/import/pull-dkr.c @@ -592,8 +592,7 @@ static int dkr_pull_pull_layer_v2(DkrPull *i) { i->current_ancestry++; - free(path); - path = NULL; + path = mfree(path); } log_info("Pulling layer %s...", layer); @@ -652,8 +651,7 @@ static int dkr_pull_pull_layer(DkrPull *i) { i->current_ancestry++; - free(path); - path = NULL; + path = mfree(path); } log_info("Pulling layer %s...", layer); diff --git a/src/journal/catalog.c b/src/journal/catalog.c index 33b0539315..a3e51e2f52 100644 --- a/src/journal/catalog.c +++ b/src/journal/catalog.c @@ -263,8 +263,7 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) { if (r < 0) return r; - free(lang); - lang = NULL; + lang = mfree(lang); } if (with_language) { diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c index fc49b2e174..098f62af50 100644 --- a/src/journal/coredumpctl.c +++ b/src/journal/coredumpctl.c @@ -587,8 +587,7 @@ static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) { if (filename && access(filename, R_OK) < 0) { log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, "File %s is not readable: %m", filename); - free(filename); - filename = NULL; + filename = mfree(filename); } if (filename && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) { diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 073cc77711..355c4f1ee3 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1259,8 +1259,7 @@ static int add_units(sd_journal *j) { } } - strv_free(patterns); - patterns = NULL; + patterns = strv_free(patterns); STRV_FOREACH(i, arg_user_units) { _cleanup_free_ char *u = NULL; diff --git a/src/libsystemd/sd-bus/bus-control.c b/src/libsystemd/sd-bus/bus-control.c index c53666ddd0..773c264cf0 100644 --- a/src/libsystemd/sd-bus/bus-control.c +++ b/src/libsystemd/sd-bus/bus-control.c @@ -256,9 +256,7 @@ static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) { name_list = (struct kdbus_info *) ((uint8_t *) bus->kdbus_buffer + cmd.offset); KDBUS_FOREACH(name, name_list, cmd.list_size) { - struct kdbus_item *item; - const char *entry_name = NULL; if ((flags & KDBUS_LIST_UNIQUE) && name->id != previous_id) { char *n; @@ -275,15 +273,15 @@ static int kernel_get_list(sd_bus *bus, uint64_t flags, char ***x) { previous_id = name->id; } - KDBUS_ITEM_FOREACH(item, name, items) - if (item->type == KDBUS_ITEM_OWNED_NAME) - entry_name = item->name.name; - - if (entry_name && service_name_is_valid(entry_name)) { - r = strv_extend(x, entry_name); - if (r < 0) { - r = -ENOMEM; - goto fail; + KDBUS_ITEM_FOREACH(item, name, items) { + if (item->type == KDBUS_ITEM_OWNED_NAME) { + if (service_name_is_valid(item->name.name)) { + r = strv_extend(x, item->name.name); + if (r < 0) { + r = -ENOMEM; + goto fail; + } + } } } } diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c index e4cbd793ed..18c36ce243 100644 --- a/src/libsystemd/sd-bus/bus-match.c +++ b/src/libsystemd/sd-bus/bus-match.c @@ -861,8 +861,7 @@ int bus_match_parse( if (r < 0) goto fail; - free(value); - value = NULL; + value = mfree(value); } else u = 0; diff --git a/src/libsystemd/sd-bus/busctl-introspect.c b/src/libsystemd/sd-bus/busctl-introspect.c index 15c10da7e9..03e83d08a1 100644 --- a/src/libsystemd/sd-bus/busctl-introspect.c +++ b/src/libsystemd/sd-bus/busctl-introspect.c @@ -462,9 +462,8 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth } } - free(argument_type); - free(argument_direction); - argument_type = argument_direction = NULL; + argument_type = mfree(argument_type); + argument_direction = mfree(argument_direction); } state = STATE_METHOD; @@ -604,8 +603,7 @@ static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth if (!strextend(&context->member_signature, argument_type, NULL)) return log_oom(); - free(argument_type); - argument_type = NULL; + argument_type = mfree(argument_type); } state = STATE_SIGNAL; diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c index 6aaaf0e5ec..a1f0f30d6c 100644 --- a/src/libsystemd/sd-bus/busctl.c +++ b/src/libsystemd/sd-bus/busctl.c @@ -656,28 +656,15 @@ static int member_compare_func(const void *a, const void *b) { assert(x->type); assert(y->type); - if (!x->interface && y->interface) - return -1; - if (x->interface && !y->interface) - return 1; - if (x->interface && y->interface) { - d = strcmp(x->interface, y->interface); - if (d != 0) - return d; - } + d = strcmp_ptr(x->interface, y->interface); + if (d != 0) + return d; d = strcmp(x->type, y->type); if (d != 0) return d; - if (!x->name && y->name) - return -1; - if (x->name && !y->name) - return 1; - if (x->name && y->name) - return strcmp(x->name, y->name); - - return 0; + return strcmp_ptr(x->name, y->name); } static int member_compare_funcp(const void *a, const void *b) { @@ -1697,6 +1684,7 @@ static int help(void) { " --acquired Only show acquired names\n" " --activatable Only show activatable names\n" " --match=MATCH Only show matching messages\n" + " --size=SIZE Maximum length of captured packet\n" " --list Don't show tree, but simple object path list\n" " --quiet Don't show method call reply\n" " --verbose Show result values in long format\n" @@ -1837,7 +1825,7 @@ static int parse_argv(int argc, char *argv[]) { case ARG_SIZE: { off_t o; - r = parse_size(optarg, 0, &o); + r = parse_size(optarg, 1024, &o); if (r < 0) { log_error("Failed to parse size: %s", optarg); return r; @@ -2018,15 +2006,15 @@ int main(int argc, char *argv[]) { } } + r = sd_bus_set_bus_client(bus, true); + if (r < 0) { + log_error_errno(r, "Failed to set bus client: %m"); + goto finish; + } + if (arg_address) r = sd_bus_set_address(bus, arg_address); else { - r = sd_bus_set_bus_client(bus, true); - if (r < 0) { - log_error_errno(r, "Failed to set bus client: %m"); - goto finish; - } - switch (arg_transport) { case BUS_TRANSPORT_LOCAL: diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 0ca225c617..0f075907d5 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -73,13 +73,9 @@ static void bus_close_fds(sd_bus *b) { detach_io_events(b); - if (b->input_fd >= 0) - safe_close(b->input_fd); - - if (b->output_fd >= 0 && b->output_fd != b->input_fd) + if (b->input_fd != b->output_fd) safe_close(b->output_fd); - - b->input_fd = b->output_fd = -1; + b->output_fd = b->input_fd = safe_close(b->input_fd); } static void bus_reset_queues(sd_bus *b) { @@ -88,15 +84,13 @@ static void bus_reset_queues(sd_bus *b) { while (b->rqueue_size > 0) sd_bus_message_unref(b->rqueue[--b->rqueue_size]); - free(b->rqueue); - b->rqueue = NULL; + b->rqueue = mfree(b->rqueue); b->rqueue_allocated = 0; while (b->wqueue_size > 0) sd_bus_message_unref(b->wqueue[--b->wqueue_size]); - free(b->wqueue); - b->wqueue = NULL; + b->wqueue = mfree(b->wqueue); b->wqueue_allocated = 0; } @@ -896,10 +890,9 @@ static int parse_container_kernel_address(sd_bus *b, const char **p, char **guid } else b->nspid = 0; - free(b->kernel); - b->kernel = strdup("/sys/fs/kdbus/0-system/bus"); - if (!b->kernel) - return -ENOMEM; + r = free_and_strdup(&b->kernel, "/sys/fs/kdbus/0-system/bus"); + if (r < 0) + return r; return 0; } @@ -909,15 +902,11 @@ static void bus_reset_parsed_address(sd_bus *b) { zero(b->sockaddr); b->sockaddr_size = 0; - strv_free(b->exec_argv); - free(b->exec_path); - b->exec_path = NULL; - b->exec_argv = NULL; + b->exec_argv = strv_free(b->exec_argv); + b->exec_path = mfree(b->exec_path); b->server_id = SD_ID128_NULL; - free(b->kernel); - b->kernel = NULL; - free(b->machine); - b->machine = NULL; + b->kernel = mfree(b->kernel); + b->machine = mfree(b->machine); b->nspid = 0; } diff --git a/src/libsystemd/sd-bus/test-bus-proxy.c b/src/libsystemd/sd-bus/test-bus-proxy.c index 369c2f331c..aef768dc18 100644 --- a/src/libsystemd/sd-bus/test-bus-proxy.c +++ b/src/libsystemd/sd-bus/test-bus-proxy.c @@ -53,7 +53,9 @@ static int test_proxy_acquired(sd_bus_message *m, void *userdata, sd_bus_error * static void test_proxy_matched(void) { _cleanup_bus_flush_close_unref_ sd_bus *a = NULL; + _cleanup_free_ char *matchstr = NULL; TestProxyMatch match = {}; + const char *me; int r; /* open bus 'a' */ @@ -70,10 +72,17 @@ static void test_proxy_matched(void) { r = sd_bus_start(a); assert_se(r >= 0); - r = sd_bus_add_match(a, NULL, - "type='signal'," - "member='NameAcquired'", - test_proxy_acquired, &match); + r = sd_bus_get_unique_name(a, &me); + assert_se(r >= 0); + + matchstr = strjoin("type='signal'," + "member='NameAcquired'," + "destination='", + me, + "'", + NULL); + assert_se(matchstr); + r = sd_bus_add_match(a, NULL, matchstr, test_proxy_acquired, &match); assert_se(r >= 0); r = sd_bus_get_unique_name(a, &match.sender); diff --git a/src/libsystemd/sd-netlink/test-local-addresses.c b/src/libsystemd/sd-netlink/test-local-addresses.c index 38cbcfbccb..9867eec065 100644 --- a/src/libsystemd/sd-netlink/test-local-addresses.c +++ b/src/libsystemd/sd-netlink/test-local-addresses.c @@ -44,9 +44,8 @@ int main(int argc, char *argv[]) { printf("Local Addresses:\n"); print_local_addresses(a, (unsigned) n); - free(a); + a = mfree(a); - a = NULL; n = local_gateways(NULL, 0, AF_UNSPEC, &a); assert_se(n >= 0); diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 9709eca9bd..5fa98e069f 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -372,11 +372,9 @@ static int prop_map_first_of_struct(sd_bus *bus, const char *member, sd_bus_mess if (r < 0) return r; - free(*p); - *p = strdup(s); - - if (!*p) - return -ENOMEM; + r = free_and_strdup(p, s); + if (r < 0) + return r; } else { r = sd_bus_message_read_basic(m, contents[0], userdata); if (r < 0) diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 397952e7e5..b0cd85e985 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -725,15 +725,13 @@ static int method_create_session(sd_bus_message *message, void *userdata, sd_bus log_warning("Existing logind session ID %s used by new audit session, ignoring", id); audit_id = 0; - free(id); - id = NULL; + id = mfree(id); } } if (!id) { do { - free(id); - id = NULL; + id = mfree(id); if (asprintf(&id, "c%lu", ++m->session_counter) < 0) return -ENOMEM; @@ -2355,8 +2353,7 @@ static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error return r; do { - free(id); - id = NULL; + id = mfree(id); if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0) return -ENOMEM; diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 2537d02845..9a2da7906e 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -636,6 +636,9 @@ int session_stop(Session *s, bool force) { s->timer_event_source = sd_event_source_unref(s->timer_event_source); + if (s->seat) + seat_evict_position(s->seat, s); + /* We are going down, don't care about FIFOs anymore */ session_remove_fifo(s); @@ -672,6 +675,9 @@ int session_finalize(Session *s) { s->timer_event_source = sd_event_source_unref(s->timer_event_source); + if (s->seat) + seat_evict_position(s->seat, s); + /* Kill session devices */ while ((sd = hashmap_first(s->devices))) session_device_free(sd); diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 9d570e0146..78e96c4e5b 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -2059,10 +2059,9 @@ int link_update(Link *link, sd_netlink_message *m) { link_free_carrier_maps(link); - free(link->ifname); - link->ifname = strdup(ifname); - if (!link->ifname) - return -ENOMEM; + r = free_and_strdup(&link->ifname, ifname); + if (r < 0) + return r; r = link_new_carrier_maps(link); if (r < 0) diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 65b9a5071b..e78b15a8b3 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -309,8 +309,7 @@ static void custom_mount_free_all(void) { strv_free(m->lower); } - free(arg_custom_mounts); - arg_custom_mounts = NULL; + arg_custom_mounts = mfree(arg_custom_mounts); arg_n_custom_mounts = 0; } @@ -503,9 +502,8 @@ static int parse_argv(int argc, char *argv[]) { break; case 'u': - free(arg_user); - arg_user = strdup(optarg); - if (!arg_user) + r = free_and_strdup(&arg_user, optarg); + if (r < 0) return log_oom(); break; @@ -562,8 +560,7 @@ static int parse_argv(int argc, char *argv[]) { case 'M': if (isempty(optarg)) { - free(arg_machine); - arg_machine = NULL; + arg_machine = mfree(arg_machine); } else { if (!machine_name_is_valid(optarg)) { log_error("Invalid machine name: %s", optarg); diff --git a/src/nss-myhostname/nss-myhostname.c b/src/nss-myhostname/nss-myhostname.c index 31db1aaf68..ab96cb231e 100644 --- a/src/nss-myhostname/nss-myhostname.c +++ b/src/nss-myhostname/nss-myhostname.c @@ -464,8 +464,7 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( } } - free(addresses); - addresses = NULL; + addresses = mfree(addresses); n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses); if (n_addresses > 0) { diff --git a/src/run/run.c b/src/run/run.c index 148854a9b5..3dd97022de 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -1129,13 +1129,9 @@ int main(int argc, char* argv[]) { } if (arg_unit && isempty(description)) { - free(description); - description = strdup(arg_unit); - - if (!description) { - r = log_oom(); + r = free_and_strdup(&description, arg_unit); + if (r < 0) goto finish; - } } arg_description = description; diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index 7370c786f9..d99aa1d6e9 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -333,8 +333,7 @@ int config_parse(const char *unit, return -ENOMEM; } - free(continuation); - continuation = NULL; + continuation = mfree(continuation); p = c; } else p = l; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 6db4d6587a..4e850ea1cf 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -3327,8 +3327,7 @@ static void print_status_info( if (! dir || last) { printf(dir ? " " : " Drop-In: "); - free(dir); - dir = NULL; + dir = mfree(dir); if (path_get_parent(*dropin, &dir) < 0) { log_oom(); diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index d7ba482834..3a92d120d2 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -704,8 +704,7 @@ static int write_files(void) { goto finish; } - free(group_tmp); - group_tmp = NULL; + group_tmp = mfree(group_tmp); } if (gshadow) { if (rename(gshadow_tmp, gshadow_path) < 0) { @@ -713,8 +712,7 @@ static int write_files(void) { goto finish; } - free(gshadow_tmp); - gshadow_tmp = NULL; + gshadow_tmp = mfree(gshadow_tmp); } } @@ -724,8 +722,7 @@ static int write_files(void) { goto finish; } - free(passwd_tmp); - passwd_tmp = NULL; + passwd_tmp = mfree(passwd_tmp); } if (shadow) { if (rename(shadow_tmp, shadow_path) < 0) { @@ -733,8 +730,7 @@ static int write_files(void) { goto finish; } - free(shadow_tmp); - shadow_tmp = NULL; + shadow_tmp = mfree(shadow_tmp); } r = 0; @@ -891,8 +887,10 @@ static int add_user(Item *i) { i->uid = p->pw_uid; i->uid_set = true; - free(i->description); - i->description = strdup(p->pw_gecos); + r = free_and_strdup(&i->description, p->pw_gecos); + if (r < 0) + return log_oom(); + return 0; } if (!IN_SET(errno, 0, ENOENT)) @@ -1149,9 +1147,8 @@ static int process_item(Item *i) { } if (i->gid_path) { - free(j->gid_path); - j->gid_path = strdup(i->gid_path); - if (!j->gid_path) + r = free_and_strdup(&j->gid_path, i->gid_path); + if (r < 0) return log_oom(); } @@ -1409,10 +1406,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify name */ - if (isempty(name) || streq(name, "-")) { - free(name); - name = NULL; - } + if (isempty(name) || streq(name, "-")) + name = mfree(name); if (name) { r = specifier_printf(name, specifier_table, NULL, &resolved_name); @@ -1428,10 +1423,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify id */ - if (isempty(id) || streq(id, "-")) { - free(id); - id = NULL; - } + if (isempty(id) || streq(id, "-")) + id = mfree(id); if (id) { r = specifier_printf(id, specifier_table, NULL, &resolved_id); @@ -1442,10 +1435,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify description */ - if (isempty(description) || streq(description, "-")) { - free(description); - description = NULL; - } + if (isempty(description) || streq(description, "-")) + description = mfree(description); if (description) { if (!valid_gecos(description)) { @@ -1455,10 +1446,8 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { } /* Verify home */ - if (isempty(home) || streq(home, "-")) { - free(home); - home = NULL; - } + if (isempty(home) || streq(home, "-")) + home = mfree(home); if (home) { if (!valid_home(home)) { diff --git a/src/test/test-af-list.c b/src/test/test-af-list.c new file mode 100644 index 0000000000..d69104f540 --- /dev/null +++ b/src/test/test-af-list.c @@ -0,0 +1,48 @@ +/*** + This file is part of systemd + + Copyright 2015 Daniel Mack + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <sys/socket.h> +#include <string.h> + +#include "macro.h" +#include "util.h" + +static const struct af_name* lookup_af(register const char *str, register unsigned int len); + +#include "af-list.h" +#include "af-to-name.h" +#include "af-from-name.h" + +int main(int argc, const char *argv[]) { + + unsigned int i; + + for (i = 0; i < ELEMENTSOF(af_names); i++) { + if (af_names[i]) { + assert_se(streq(af_to_name(i), af_names[i])); + assert_se(af_from_name(af_names[i]) == (int) i); + } + } + + assert_se(af_to_name(af_max()) == NULL); + assert_se(af_to_name(-1) == NULL); + assert_se(af_from_name("huddlduddl") == AF_UNSPEC); + + return 0; +}
\ No newline at end of file diff --git a/src/test/test-arphrd-list.c b/src/test/test-arphrd-list.c new file mode 100644 index 0000000000..d7c8eaa4a9 --- /dev/null +++ b/src/test/test-arphrd-list.c @@ -0,0 +1,48 @@ +/*** + This file is part of systemd + + Copyright 2015 Daniel Mack + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <net/if_arp.h> +#include <string.h> + +#include "macro.h" +#include "util.h" + +static const struct arphrd_name* lookup_arphrd(register const char *str, register unsigned int len); + +#include "arphrd-list.h" +#include "arphrd-to-name.h" +#include "arphrd-from-name.h" + +int main(int argc, const char *argv[]) { + + unsigned int i; + + for (i = 1; i < ELEMENTSOF(arphrd_names); i++) { + if (arphrd_names[i]) { + assert_se(streq(arphrd_to_name(i), arphrd_names[i])); + assert_se(arphrd_from_name(arphrd_names[i]) == (int) i); + } + } + + assert_se(arphrd_to_name(arphrd_max()) == NULL); + assert_se(arphrd_to_name(0) == NULL); + assert_se(arphrd_from_name("huddlduddl") == 0); + + return 0; +}
\ No newline at end of file diff --git a/src/test/test-bitmap.c b/src/test/test-bitmap.c index 96deeded7e..ff22117745 100644 --- a/src/test/test-bitmap.c +++ b/src/test/test-bitmap.c @@ -20,7 +20,7 @@ #include "bitmap.h" int main(int argc, const char *argv[]) { - _cleanup_bitmap_free_ Bitmap *b = NULL; + _cleanup_bitmap_free_ Bitmap *b = NULL, *b2 = NULL; Iterator it; unsigned n = (unsigned) -1, i = 0; @@ -101,5 +101,23 @@ int main(int argc, const char *argv[]) { assert_se(bitmap_set(b, (unsigned) -1) == -ERANGE); + bitmap_free(b); + b = NULL; + assert_se(bitmap_ensure_allocated(&b) == 0); + assert_se(bitmap_ensure_allocated(&b2) == 0); + + assert_se(bitmap_equal(b, b2)); + assert_se(bitmap_set(b, 0) == 0); + bitmap_unset(b, 0); + assert_se(bitmap_equal(b, b2)); + + assert_se(bitmap_set(b, 1) == 0); + bitmap_clear(b); + assert_se(bitmap_equal(b, b2)); + + assert_se(bitmap_set(b, 0) == 0); + assert_se(bitmap_set(b2, 0) == 0); + assert_se(bitmap_equal(b, b2)); + return 0; } diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c index f257af445a..2c18090ae5 100644 --- a/src/test/test-socket-util.c +++ b/src/test/test-socket-util.c @@ -158,6 +158,20 @@ static void test_socket_address_is_netlink(void) { assert_se(!socket_address_is_netlink(&a, "route 1")); } +static void test_in_addr_is_null(void) { + + union in_addr_union i = {}; + + assert_se(in_addr_is_null(AF_INET, &i) == true); + assert_se(in_addr_is_null(AF_INET6, &i) == true); + + i.in.s_addr = 0x1000000; + assert_se(in_addr_is_null(AF_INET, &i) == false); + assert_se(in_addr_is_null(AF_INET6, &i) == false); + + assert_se(in_addr_is_null(-1, &i) == -EAFNOSUPPORT); +} + static void test_in_addr_prefix_intersect_one(unsigned f, const char *a, unsigned apl, const char *b, unsigned bpl, int result) { union in_addr_union ua, ub; @@ -340,6 +354,7 @@ int main(int argc, char *argv[]) { test_socket_address_is(); test_socket_address_is_netlink(); + test_in_addr_is_null(); test_in_addr_prefix_intersect(); test_in_addr_prefix_next(); test_in_addr_to_string(); diff --git a/src/test/test-util.c b/src/test/test-util.c index f43433baa1..3e87e9e710 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -280,6 +280,39 @@ static void test_parse_uid(void) { r = parse_uid("100", &uid); assert_se(r == 0); assert_se(uid == 100); + + r = parse_uid("65535", &uid); + assert_se(r == -ENXIO); +} + +static void test_safe_atou16(void) { + int r; + uint16_t l; + + r = safe_atou16("12345", &l); + assert_se(r == 0); + assert_se(l == 12345); + + r = safe_atou16("123456", &l); + assert_se(r == -ERANGE); + + r = safe_atou16("junk", &l); + assert_se(r == -EINVAL); +} + +static void test_safe_atoi16(void) { + int r; + int16_t l; + + r = safe_atoi16("-12345", &l); + assert_se(r == 0); + assert_se(l == -12345); + + r = safe_atoi16("36536", &l); + assert_se(r == -ERANGE); + + r = safe_atoi16("junk", &l); + assert_se(r == -EINVAL); } static void test_safe_atolli(void) { @@ -583,6 +616,15 @@ static void test_unbase32hexmem(void) { assert_se(unbase32hexmem("AAAAB===", strlen("AAAAB==="), true, &mem, &len) == -EINVAL); assert_se(unbase32hexmem("AAAAAAB=", strlen("AAAAAAB="), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("XPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CXNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPXMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNXUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNMXOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNMUXJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNMUOX1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNMUOJX", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("", strlen(""), false, &mem, &len) == 0); assert_se(streq(strndupa(mem, len), "")); free(mem); @@ -714,45 +756,38 @@ static void test_cunescape(void) { assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", 0, &unescaped) < 0); assert_se(cunescape("abc\\\\\\\"\\b\\f\\a\\n\\r\\t\\v\\003\\177\\234\\313\\000\\x00", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "abc\\\"\b\f\a\n\r\t\v\003\177\234\313\\000\\x00")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); /* incomplete sequences */ assert_se(cunescape("\\x0", 0, &unescaped) < 0); assert_se(cunescape("\\x0", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\x0")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\x", 0, &unescaped) < 0); assert_se(cunescape("\\x", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\x")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\", 0, &unescaped) < 0); assert_se(cunescape("\\", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\11", 0, &unescaped) < 0); assert_se(cunescape("\\11", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\11")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\1", 0, &unescaped) < 0); assert_se(cunescape("\\1", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "\\1")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\u0000", 0, &unescaped) < 0); assert_se(cunescape("\\u00DF\\U000000df\\u03a0\\U00000041", UNESCAPE_RELAX, &unescaped) >= 0); assert_se(streq_ptr(unescaped, "ßßΠA")); - free(unescaped); - unescaped = NULL; + unescaped = mfree(unescaped); assert_se(cunescape("\\073", 0, &unescaped) >= 0); assert_se(streq_ptr(unescaped, ";")); @@ -859,32 +894,28 @@ static void test_read_hostname_config(void) { write_string_file(path, "foo", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(streq(hostname, "foo")); - free(hostname); - hostname = NULL; + hostname = mfree(hostname); /* with comment */ write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foo")); - free(hostname); - hostname = NULL; + hostname = mfree(hostname); /* with comment and extra whitespace */ write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foo")); - free(hostname); - hostname = NULL; + hostname = mfree(hostname); /* cleans up name */ write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foobar.com")); - free(hostname); - hostname = NULL; + hostname = mfree(hostname); /* no value set */ hostname = (char*) 0x1234; @@ -1260,6 +1291,16 @@ static void test_endswith(void) { assert_se(!endswith("foobar", "foobarfoofoo")); } +static void test_endswith_no_case(void) { + assert_se(endswith_no_case("fooBAR", "bar")); + assert_se(endswith_no_case("foobar", "")); + assert_se(endswith_no_case("foobar", "FOOBAR")); + assert_se(endswith_no_case("", "")); + + assert_se(!endswith_no_case("foobar", "FOO")); + assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO")); +} + static void test_close_nointr(void) { char name[] = "/tmp/test-test-close_nointr.XXXXXX"; int fd; @@ -2083,6 +2124,18 @@ static void test_tempfn(void) { free(ret); } +static void test_strcmp_ptr(void) { + assert_se(strcmp_ptr(NULL, NULL) == 0); + assert_se(strcmp_ptr("", NULL) > 0); + assert_se(strcmp_ptr("foo", NULL) > 0); + assert_se(strcmp_ptr(NULL, "") < 0); + assert_se(strcmp_ptr(NULL, "bar") < 0); + assert_se(strcmp_ptr("foo", "bar") > 0); + assert_se(strcmp_ptr("bar", "baz") < 0); + assert_se(strcmp_ptr("foo", "foo") == 0); + assert_se(strcmp_ptr("", "") == 0); +} + int main(int argc, char *argv[]) { log_parse_environment(); log_open(); @@ -2098,6 +2151,8 @@ int main(int argc, char *argv[]) { test_parse_boolean(); test_parse_pid(); test_parse_uid(); + test_safe_atou16(); + test_safe_atoi16(); test_safe_atolli(); test_safe_atod(); test_strappend(); @@ -2147,6 +2202,7 @@ int main(int argc, char *argv[]) { test_is_valid_documentation_url(); test_file_in_same_dir(); test_endswith(); + test_endswith_no_case(); test_close_nointr(); test_unlink_noerrno(); test_readlink_and_make_absolute(); @@ -2169,6 +2225,7 @@ int main(int argc, char *argv[]) { test_shell_maybe_quote(); test_parse_mode(); test_tempfn(); + test_strcmp_ptr(); return 0; } diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c index b030206948..7b4178c993 100644 --- a/src/timesync/timesyncd.c +++ b/src/timesync/timesyncd.c @@ -113,10 +113,6 @@ int main(int argc, char *argv[]) { if (r < 0) goto finish; - /* We need one process for ourselves, plus one thread for the asynchronous resolver */ - if (setrlimit(RLIMIT_NPROC, &RLIMIT_MAKE_CONST(2)) < 0) - log_warning_errno(errno, "Failed to lower RLIMIT_NPROC to 2: %m"); - assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0); r = manager_new(&m); diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 73b19d8e89..82cbf95f1e 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -162,8 +162,7 @@ static int ask_password_plymouth( /* Hmm, first try with cached * passwords failed, so let's retry * with a normal password request */ - free(packet); - packet = NULL; + packet = mfree(packet); if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) return -ENOMEM; diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c index 4ca0a69d7d..aa5cda6fe7 100644 --- a/src/udev/udev-builtin-path_id.c +++ b/src/udev/udev-builtin-path_id.c @@ -674,20 +674,16 @@ static int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool * might produce conflicting IDs if the parent does not provide a * unique and predictable name. */ - if (!supported_parent) { - free(path); - path = NULL; - } + if (!supported_parent) + path = mfree(path); /* * Do not return block devices without a well-known transport. Some * devices do not expose their buses and do not provide a unique * and predictable name that way. */ - if (streq(udev_device_get_subsystem(dev), "block") && !supported_transport) { - free(path); - path = NULL; - } + if (streq(udev_device_get_subsystem(dev), "block") && !supported_transport) + path = mfree(path); out: if (path != NULL) { diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index d00f90afa6..368da9b1ae 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -2429,8 +2429,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, rules_str(rules, rule->rule.filename_off), rule->rule.filename_line); break; } - free(event->name); - event->name = strdup(name_str); + free_and_strdup(&event->name, name_str); log_debug("NAME '%s' %s:%u", event->name, rules_str(rules, rule->rule.filename_off), @@ -2590,8 +2589,7 @@ int udev_rules_apply_static_dev_perms(struct udev_rules *rules) { uid = 0; gid = 0; mode = 0; - strv_free(tags); - tags = NULL; + tags = strv_free(tags); break; case TK_A_OWNER_ID: uid = cur->key.uid; |