From d737b451fe64b659bfe1128052c9d4cb129f4bf0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 2 Apr 2019 14:48:50 +0200 Subject: [PATCH 1/9] analyze: use empty_or_root() where appropriate --- src/analyze/analyze-security.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index a978ed6da8..2917aff823 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -296,10 +296,8 @@ static int assess_root_directory( assert(ret_description); *ret_badness = - (isempty(info->root_directory) || - path_equal(info->root_directory, "/")) && - (isempty(info->root_image) || - path_equal(info->root_image, "/")); + empty_or_root(info->root_directory) || + empty_or_root(info->root_image); *ret_description = NULL; return 0; From 0c21dafb54dea7564c6a0f063492926f495006b9 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 2 Apr 2019 14:49:28 +0200 Subject: [PATCH 2/9] util-lib: use FLAGS_SET() where appropriate --- src/basic/fs-util.c | 4 ++-- src/shared/bus-util.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 9f3fd45691..d1c06cf12a 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -1411,9 +1411,9 @@ int open_parent(const char *path, int flags, mode_t mode) { /* Let's insist on O_DIRECTORY since the parent of a file or directory is a directory. Except if we open an * O_TMPFILE file, because in that case we are actually create a regular file below the parent directory. */ - if ((flags & O_PATH) == O_PATH) + if (FLAGS_SET(flags, O_PATH)) flags |= O_DIRECTORY; - else if ((flags & O_TMPFILE) != O_TMPFILE) + else if (!FLAGS_SET(flags, O_TMPFILE)) flags |= O_DIRECTORY|O_RDONLY; fd = open(parent, flags, mode); diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 1911dd0ce6..00120e9874 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -756,7 +756,7 @@ static int bus_print_property(const char *name, const char *expected_value, sd_b if ((u & NAMESPACE_FLAGS_ALL) == 0) result = "yes"; - else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL) + else if (FLAGS_SET(u, NAMESPACE_FLAGS_ALL)) result = "no"; else { r = namespace_flags_to_string(u, &s); From a7798cd81b11205811e8d6e848e23b8b285f2bd8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 2 Apr 2019 14:50:15 +0200 Subject: [PATCH 3/9] tree-wide: use reallocarray() where appropriate --- src/boot/bootctl.c | 2 +- src/libsystemd/sd-login/sd-login.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/boot/bootctl.c b/src/boot/bootctl.c index 381c234f3b..a7de3800fc 100644 --- a/src/boot/bootctl.c +++ b/src/boot/bootctl.c @@ -675,7 +675,7 @@ static int insert_into_order(uint16_t slot, bool first) { } /* extend array */ - t = realloc(order, (n + 1) * sizeof(uint16_t)); + t = reallocarray(order, n + 1, sizeof(uint16_t)); if (!t) return -ENOMEM; order = t; diff --git a/src/libsystemd/sd-login/sd-login.c b/src/libsystemd/sd-login/sd-login.c index 07f21e84de..0dc368e6e2 100644 --- a/src/libsystemd/sd-login/sd-login.c +++ b/src/libsystemd/sd-login/sd-login.c @@ -818,7 +818,7 @@ _public_ int sd_get_uids(uid_t **users) { uid_t *t; n = MAX(16, 2*r); - t = realloc(l, sizeof(uid_t) * n); + t = reallocarray(l, sizeof(uid_t), n); if (!t) return -ENOMEM; From c1db999eb8774463ce1fe3fe24e7fffbdc1ccb16 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 2 Apr 2019 14:50:36 +0200 Subject: [PATCH 4/9] boot: use TAKE_PTR() where appropriate --- src/boot/efi/boot.c | 21 +++++++-------------- src/boot/efi/util.c | 3 +-- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index 7b3e782454..57c423bfb6 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -269,10 +269,8 @@ static BOOLEAN line_edit( case KEYPRESS(0, 0, CHAR_LINEFEED): case KEYPRESS(0, 0, CHAR_CARRIAGE_RETURN): - if (StrCmp(line, line_in) != 0) { - *line_out = line; - line = NULL; - } + if (StrCmp(line, line_in) != 0) + *line_out = TAKE_PTR(line); enter = TRUE; exit = TRUE; break; @@ -1258,8 +1256,7 @@ static VOID config_entry_bump_counters( /* If the file we just renamed is the loader path, then let's update that. */ if (StrCmp(entry->loader, old_path) == 0) { FreePool(entry->loader); - entry->loader = new_path; - new_path = NULL; + entry->loader = TAKE_PTR(new_path); } } @@ -1360,10 +1357,8 @@ static VOID config_entry_add_from_file( s = PoolPrint(L"%s %s", entry->options, new); FreePool(entry->options); entry->options = s; - } else { - entry->options = new; - new = NULL; - } + } else + entry->options = TAKE_PTR(new); continue; } @@ -1382,10 +1377,8 @@ static VOID config_entry_add_from_file( s = PoolPrint(L"%s %s", initrd, entry->options); FreePool(entry->options); entry->options = s; - } else { - entry->options = initrd; - initrd = NULL; - } + } else + entry->options = TAKE_PTR(initrd); } entry->device = device; diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index bc1d4ae5c4..4134e2200a 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -122,8 +122,7 @@ EFI_STATUS efivar_get(const CHAR16 *name, CHAR16 **value) { /* Return buffer directly if it happens to be NUL terminated already */ if (size >= 2 && buf[size-2] == 0 && buf[size-1] == 0) { - *value = (CHAR16*) buf; - buf = NULL; + *value = (CHAR16*) TAKE_PTR(buf); return EFI_SUCCESS; } From c614711386764dbb2b5bb082f708da02bddb3947 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 2 Apr 2019 14:51:48 +0200 Subject: [PATCH 5/9] tree-wide: use SYNTHETIC_ERRNO() where appropriate --- src/cgtop/cgtop.c | 7 ++-- src/nspawn/nspawn.c | 90 ++++++++++++++-------------------------- src/resolve/resolvectl.c | 6 +-- 3 files changed, 36 insertions(+), 67 deletions(-) diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index eb9ccd0cb0..f27c80120d 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -922,10 +922,9 @@ static int run(int argc, char *argv[]) { arg_count = (mask & CGROUP_MASK_PIDS) ? COUNT_PIDS : COUNT_USERSPACE_PROCESSES; - if (arg_recursive_unset && arg_count == COUNT_PIDS) { - log_error("Non-recursive counting is only supported when counting processes, not tasks. Use -P or -k."); - return -EINVAL; - } + if (arg_recursive_unset && arg_count == COUNT_PIDS) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Non-recursive counting is only supported when counting processes, not tasks. Use -P or -k."); r = show_cgroup_get_path_and_warn(arg_machine, arg_root, &root); if (r < 0) diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 7c5d9d0abd..5ab4b480b9 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -949,10 +949,8 @@ static int parse_argv(int argc, char *argv[]) { case ARG_LINK_JOURNAL: r = parse_link_journal(optarg, &arg_link_journal, &arg_link_journal_try); - if (r < 0) { - log_error_errno(r, "Failed to parse link journal mode %s", optarg); - return -EINVAL; - } + if (r < 0) + return log_error_errno(r, "Failed to parse link journal mode %s", optarg); arg_settings_mask |= SETTING_LINK_JOURNAL; break; @@ -1243,9 +1241,8 @@ static int parse_argv(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to parse root hash: %s", optarg); if (l < sizeof(sd_id128_t)) { - log_error("Root hash must be at least 128bit long: %s", optarg); free(k); - return -EINVAL; + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Root hash must be at least 128bit long: %s", optarg); } free(arg_root_hash); @@ -1389,10 +1386,8 @@ static int parse_argv(int argc, char *argv[]) { "read-only\n" "passive\n" "pipe"); - else { - log_error("Unknown console mode: %s", optarg); - return -EINVAL; - } + else + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown console mode: %s", optarg); arg_settings_mask |= SETTING_CONSOLE_MODE; break; @@ -2618,10 +2613,8 @@ static int determine_names(void) { return log_error_errno(r, "Failed to determine current directory: %m"); } - if (!arg_directory && !arg_image) { - log_error("Failed to determine path, please use -D or -i."); - return -EINVAL; - } + if (!arg_directory && !arg_image) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine path, please use -D or -i."); } if (!arg_machine) { @@ -2644,10 +2637,8 @@ static int determine_names(void) { return log_oom(); hostname_cleanup(arg_machine); - if (!machine_name_is_valid(arg_machine)) { - log_error("Failed to determine machine name automatically, please use -M."); - return -EINVAL; - } + if (!machine_name_is_valid(arg_machine)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to determine machine name automatically, please use -M."); if (arg_ephemeral) { char *b; @@ -2774,10 +2765,8 @@ static int patch_sysctl(void) { break; } - if (!good) { - log_error("Refusing to write to sysctl '%s', as it is not safe in the selected namespaces.", *k); - return -EPERM; - } + if (!good) + return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Refusing to write to sysctl '%s', as it is not safe in the selected namespaces.", *k); r = sysctl_write(*k, *v); if (r < 0) @@ -4162,10 +4151,9 @@ static int run_container(int master, log_debug_errno(r, "Cannot determine if passed network namespace path '%s' really refers to a network namespace, assuming it does.", arg_network_namespace_path); else if (r < 0) return log_error_errno(r, "Failed to check %s fs type: %m", arg_network_namespace_path); - else if (r == 0) { - log_error("Path %s doesn't refer to a network namespace, refusing.", arg_network_namespace_path); - return -EINVAL; - } + else if (r == 0) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "Path %s doesn't refer to a network namespace, refusing.", arg_network_namespace_path); } *pid = raw_clone(SIGCHLD|CLONE_NEWNS); @@ -4228,10 +4216,8 @@ static int run_container(int master, l = recv(uid_shift_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, 0); if (l < 0) return log_error_errno(errno, "Failed to read UID shift: %m"); - if (l != sizeof arg_uid_shift) { - log_error("Short read while reading UID shift."); - return -EIO; - } + if (l != sizeof arg_uid_shift) + return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading UID shift."); if (arg_userns_mode == USER_NAMESPACE_PICK) { /* If we are supposed to pick the UID shift, let's try to use the shift read from the @@ -4245,10 +4231,8 @@ static int run_container(int master, l = send(uid_shift_socket_pair[0], &arg_uid_shift, sizeof arg_uid_shift, MSG_NOSIGNAL); if (l < 0) return log_error_errno(errno, "Failed to send UID shift: %m"); - if (l != sizeof arg_uid_shift) { - log_error("Short write while writing UID shift."); - return -EIO; - } + if (l != sizeof arg_uid_shift) + return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write while writing UID shift."); } } @@ -4257,11 +4241,9 @@ static int run_container(int master, l = recv(unified_cgroup_hierarchy_socket_pair[0], &arg_unified_cgroup_hierarchy, sizeof(arg_unified_cgroup_hierarchy), 0); if (l < 0) return log_error_errno(errno, "Failed to read cgroup mode: %m"); - if (l != sizeof(arg_unified_cgroup_hierarchy)) { - log_error("Short read while reading cgroup mode (%zu bytes).%s", - l, l == 0 ? " The child is most likely dead." : ""); - return -EIO; - } + if (l != sizeof(arg_unified_cgroup_hierarchy)) + return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading cgroup mode (%zu bytes).%s", + l, l == 0 ? " The child is most likely dead." : ""); } /* Wait for the outer child. */ @@ -4275,19 +4257,15 @@ static int run_container(int master, l = recv(pid_socket_pair[0], pid, sizeof *pid, 0); if (l < 0) return log_error_errno(errno, "Failed to read inner child PID: %m"); - if (l != sizeof *pid) { - log_error("Short read while reading inner child PID."); - return -EIO; - } + if (l != sizeof *pid) + return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading inner child PID."); /* We also retrieve container UUID in case it was generated by outer child */ l = recv(uuid_socket_pair[0], &arg_uuid, sizeof arg_uuid, 0); if (l < 0) return log_error_errno(errno, "Failed to read container machine ID: %m"); - if (l != sizeof(arg_uuid)) { - log_error("Short read while reading container machined ID."); - return -EIO; - } + if (l != sizeof(arg_uuid)) + return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short read while reading container machined ID."); /* We also retrieve the socket used for notifications generated by outer child */ notify_socket = receive_one_fd(notify_socket_pair[0], 0); @@ -4298,10 +4276,8 @@ static int run_container(int master, log_debug("Init process invoked as PID "PID_FMT, *pid); if (arg_userns_mode != USER_NAMESPACE_NO) { - if (!barrier_place_and_sync(&barrier)) { /* #1 */ - log_error("Child died too early."); - return -ESRCH; - } + if (!barrier_place_and_sync(&barrier)) /* #1 */ + return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early."); r = setup_uid_map(*pid); if (r < 0) @@ -4313,10 +4289,8 @@ static int run_container(int master, if (arg_private_network) { if (!arg_network_namespace_path) { /* Wait until the child has unshared its network namespace. */ - if (!barrier_place_and_sync(&barrier)) { /* #3 */ - log_error("Child died too early"); - return -ESRCH; - } + if (!barrier_place_and_sync(&barrier)) /* #3 */ + return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early"); } r = move_network_interfaces(*pid, arg_network_interfaces); @@ -4472,10 +4446,8 @@ static int run_container(int master, return r; /* Let the child know that we are ready and wait that the child is completely ready now. */ - if (!barrier_place_and_sync(&barrier)) { /* #5 */ - log_error("Child died too early."); - return -ESRCH; - } + if (!barrier_place_and_sync(&barrier)) /* #5 */ + return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Child died too early."); /* At this point we have made use of the UID we picked, and thus nss-mymachines * will make them appear in getpwuid(), thus we can release the /etc/passwd lock. */ diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index 7b9e13f3f6..1d2fc71309 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -119,10 +119,8 @@ int ifname_mangle(const char *s) { } } - if (arg_ifindex > 0 && arg_ifindex != ifi) { - log_error("Specified multiple different interfaces. Refusing."); - return -EINVAL; - } + if (arg_ifindex > 0 && arg_ifindex != ifi) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified multiple different interfaces. Refusing."); arg_ifindex = ifi; free_and_replace(arg_ifname, iface); From 39f2bc6e7eca8e62e38d52d5d92372dd01a459a6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 2 Apr 2019 14:52:01 +0200 Subject: [PATCH 6/9] sd-device: use xsprintf() where appropriate --- src/libsystemd/sd-device/sd-device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index f455a4eb27..c2315c03fe 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -237,7 +237,7 @@ _public_ int sd_device_new_from_devnum(sd_device **ret, char type, dev_t devnum) assert_return(IN_SET(type, 'b', 'c'), -EINVAL); /* use /sys/dev/{block,char}/: link */ - snprintf(id, sizeof(id), "%u:%u", major(devnum), minor(devnum)); + xsprintf(id, "%u:%u", major(devnum), minor(devnum)); syspath = strjoina("/sys/dev/", (type == 'b' ? "block" : "char"), "/", id); From bab4820ee2e08be5a3dc42712bf71246b8cf2428 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 2 Apr 2019 14:52:17 +0200 Subject: [PATCH 7/9] sd-event: use DIV_ROUND_UP where appropriate --- src/libsystemd/sd-event/sd-event.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index eb98e49ca2..1987f279eb 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -3116,7 +3116,7 @@ _public_ int sd_event_wait(sd_event *e, uint64_t timeout) { timeout = 0; m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max, - timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC)); + timeout == (uint64_t) -1 ? -1 : (int) DIV_ROUND_UP(timeout, USEC_PER_MSEC)); if (m < 0) { if (errno == EINTR) { e->state = SD_EVENT_PENDING; From 02dab76e9371ceff3b3759fbe7b48eaa24c9a49e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 2 Apr 2019 14:52:56 +0200 Subject: [PATCH 8/9] json: use SYNTHETIC_ERRNO() where appropriate --- src/shared/json.c | 66 ++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/src/shared/json.c b/src/shared/json.c index 10431423e9..db003a41a4 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -3297,10 +3297,8 @@ int json_dispatch_boolean(const char *name, JsonVariant *variant, JsonDispatchFl assert(variant); assert(b); - if (!json_variant_is_boolean(variant)) { - json_log(variant, flags, 0, "JSON field '%s' is not a boolean.", strna(name)); - return -EINVAL; - } + if (!json_variant_is_boolean(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a boolean.", strna(name)); *b = json_variant_boolean(variant); return 0; @@ -3312,10 +3310,8 @@ int json_dispatch_tristate(const char *name, JsonVariant *variant, JsonDispatchF assert(variant); assert(b); - if (!json_variant_is_boolean(variant)) { - json_log(variant, flags, 0, "JSON field '%s' is not a boolean.", strna(name)); - return -EINVAL; - } + if (!json_variant_is_boolean(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a boolean.", strna(name)); *b = json_variant_boolean(variant); return 0; @@ -3327,10 +3323,8 @@ int json_dispatch_integer(const char *name, JsonVariant *variant, JsonDispatchFl assert(variant); assert(i); - if (!json_variant_is_integer(variant)) { - json_log(variant, flags, 0, "JSON field '%s' is not an integer.", strna(name)); - return -EINVAL; - } + if (!json_variant_is_integer(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); *i = json_variant_integer(variant); return 0; @@ -3342,10 +3336,8 @@ int json_dispatch_unsigned(const char *name, JsonVariant *variant, JsonDispatchF assert(variant); assert(u); - if (!json_variant_is_unsigned(variant)) { - json_log(variant, flags, 0, "JSON field '%s' is not an unsigned integer.", strna(name)); - return -EINVAL; - } + if (!json_variant_is_unsigned(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an unsigned integer.", strna(name)); *u = json_variant_unsigned(variant); return 0; @@ -3357,15 +3349,11 @@ int json_dispatch_uint32(const char *name, JsonVariant *variant, JsonDispatchFla assert(variant); assert(u); - if (!json_variant_is_unsigned(variant)) { - json_log(variant, flags, 0, "JSON field '%s' is not an unsigned integer.", strna(name)); - return -EINVAL; - } + if (!json_variant_is_unsigned(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an unsigned integer.", strna(name)); - if (json_variant_unsigned(variant) > UINT32_MAX) { - json_log(variant, flags, 0, "JSON field '%s' out of bounds.", strna(name)); - return -ERANGE; - } + if (json_variant_unsigned(variant) > UINT32_MAX) + return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' out of bounds.", strna(name)); *u = (uint32_t) json_variant_unsigned(variant); return 0; @@ -3377,15 +3365,11 @@ int json_dispatch_int32(const char *name, JsonVariant *variant, JsonDispatchFlag assert(variant); assert(i); - if (!json_variant_is_integer(variant)) { - json_log(variant, flags, 0, "JSON field '%s' is not an integer.", strna(name)); - return -EINVAL; - } + if (!json_variant_is_integer(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an integer.", strna(name)); - if (json_variant_integer(variant) < INT32_MIN || json_variant_integer(variant) > INT32_MAX) { - json_log(variant, flags, 0, "JSON field '%s' out of bounds.", strna(name)); - return -ERANGE; - } + if (json_variant_integer(variant) < INT32_MIN || json_variant_integer(variant) > INT32_MAX) + return json_log(variant, flags, SYNTHETIC_ERRNO(ERANGE), "JSON field '%s' out of bounds.", strna(name)); *i = (int32_t) json_variant_integer(variant); return 0; @@ -3403,10 +3387,8 @@ int json_dispatch_string(const char *name, JsonVariant *variant, JsonDispatchFla return 0; } - if (!json_variant_is_string(variant)) { - json_log(variant, flags, 0, "JSON field '%s' is not a string.", strna(name)); - return -EINVAL; - } + if (!json_variant_is_string(variant)) + return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); r = free_and_strdup(s, json_variant_string(variant)); if (r < 0) @@ -3429,20 +3411,16 @@ int json_dispatch_strv(const char *name, JsonVariant *variant, JsonDispatchFlags return 0; } - if (!json_variant_is_array(variant)) { - json_log(variant, 0, flags, "JSON field '%s' is not an array.", strna(name)); - return -EINVAL; - } + if (!json_variant_is_array(variant)) + return json_log(variant, SYNTHETIC_ERRNO(EINVAL), flags, "JSON field '%s' is not an array.", strna(name)); for (i = 0; i < json_variant_elements(variant); i++) { JsonVariant *e; assert_se(e = json_variant_by_index(variant, i)); - if (!json_variant_is_string(e)) { - json_log(e, 0, flags, "JSON array element is not a string."); - return -EINVAL; - } + if (!json_variant_is_string(e)) + return json_log(e, flags, SYNTHETIC_ERRNO(EINVAL), "JSON array element is not a string."); r = strv_extend(&l, json_variant_string(e)); if (r < 0) From 568ee8fc468d139b87ce771098b11aa15086c30f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 2 Apr 2019 14:53:05 +0200 Subject: [PATCH 9/9] udev: use strempty() where appropriate --- src/udev/udev-rules.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index c5d6feee7e..ee87d7c65c 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -1645,8 +1645,7 @@ static bool match_key(UdevRules *rules, struct token *token, const char *val) { char *pos; bool match = false; - if (!val) - val = ""; + val = strempty(val); switch (token->key.glob) { case GL_PLAIN: