From 79c397a12effad6feb7cfc4bfd66d91ac0c91065 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 21 Feb 2021 07:44:24 +0900 Subject: [PATCH 1/4] sd-device-monitor: use assert() in non-public functions --- src/libsystemd/sd-device/device-monitor.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/libsystemd/sd-device/device-monitor.c b/src/libsystemd/sd-device/device-monitor.c index 203106ab44..008522494b 100644 --- a/src/libsystemd/sd-device/device-monitor.c +++ b/src/libsystemd/sd-device/device-monitor.c @@ -83,8 +83,8 @@ static int monitor_set_nl_address(sd_device_monitor *m) { } int device_monitor_allow_unicast_sender(sd_device_monitor *m, sd_device_monitor *sender) { - assert_return(m, -EINVAL); - assert_return(sender, -EINVAL); + assert(m); + assert(sender); m->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid; return 0; @@ -104,7 +104,7 @@ int device_monitor_disconnect(sd_device_monitor *m) { } int device_monitor_get_fd(sd_device_monitor *m) { - assert_return(m, -EINVAL); + assert(m); return m->sock; } @@ -114,8 +114,8 @@ int device_monitor_new_full(sd_device_monitor **ret, MonitorNetlinkGroup group, _cleanup_close_ int sock = -1; int r; + assert(group >= 0 && group < _MONITOR_NETLINK_GROUP_MAX); assert_return(ret, -EINVAL); - assert_return(group >= 0 && group < _MONITOR_NETLINK_GROUP_MAX, -EINVAL); if (group == MONITOR_GROUP_UDEV && access("/run/udev/control", F_OK) < 0 && @@ -304,7 +304,7 @@ _public_ sd_event_source *sd_device_monitor_get_event_source(sd_device_monitor * int device_monitor_enable_receiving(sd_device_monitor *m) { int r; - assert_return(m, -EINVAL); + assert(m); r = sd_device_monitor_filter_update(m); if (r < 0) @@ -346,8 +346,8 @@ static int passes_filter(sd_device_monitor *m, sd_device *device) { const char *tag, *subsystem, *devtype, *s, *d = NULL; int r; - assert_return(m, -EINVAL); - assert_return(device, -EINVAL); + assert(m); + assert(device); if (hashmap_isempty(m->subsystem_filter)) goto tag; @@ -413,6 +413,7 @@ int device_monitor_receive_device(sd_device_monitor *m, sd_device **ret) { bool is_initialized = false; int r; + assert(m); assert(ret); buflen = recvmsg(m->sock, &smsg, 0); From 0fa28efea6dd2031d8c6d5644e16429090884dd3 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 21 Feb 2021 09:14:00 +0900 Subject: [PATCH 2/4] sd-device-monitor: use UINT64_C() macro --- src/libsystemd/sd-device/device-monitor.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libsystemd/sd-device/device-monitor.c b/src/libsystemd/sd-device/device-monitor.c index 008522494b..bf00c499d2 100644 --- a/src/libsystemd/sd-device/device-monitor.c +++ b/src/libsystemd/sd-device/device-monitor.c @@ -508,10 +508,10 @@ static uint64_t string_bloom64(const char *str) { uint64_t bits = 0; uint32_t hash = string_hash32(str); - bits |= 1LLU << (hash & 63); - bits |= 1LLU << ((hash >> 6) & 63); - bits |= 1LLU << ((hash >> 12) & 63); - bits |= 1LLU << ((hash >> 18) & 63); + bits |= UINT64_C(1) << (hash & 63); + bits |= UINT64_C(1) << ((hash >> 6) & 63); + bits |= UINT64_C(1) << ((hash >> 12) & 63); + bits |= UINT64_C(1) << ((hash >> 18) & 63); return bits; } From 7117842657c0fc5a3446b6fe158615279cf2d650 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 21 Feb 2021 08:11:06 +0900 Subject: [PATCH 3/4] sd-device-monitor: use hashmap_put_strdup_full() --- src/libsystemd/sd-device/device-monitor.c | 41 +++++++++-------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/src/libsystemd/sd-device/device-monitor.c b/src/libsystemd/sd-device/device-monitor.c index bf00c499d2..3948e862a9 100644 --- a/src/libsystemd/sd-device/device-monitor.c +++ b/src/libsystemd/sd-device/device-monitor.c @@ -334,8 +334,8 @@ static sd_device_monitor *device_monitor_free(sd_device_monitor *m) { (void) sd_device_monitor_detach_event(m); - hashmap_free_free_free(m->subsystem_filter); - set_free_free(m->tag_filter); + hashmap_free(m->subsystem_filter); + set_free(m->tag_filter); return mfree(m); } @@ -718,41 +718,32 @@ _public_ int sd_device_monitor_filter_update(sd_device_monitor *m) { } _public_ int sd_device_monitor_filter_add_match_subsystem_devtype(sd_device_monitor *m, const char *subsystem, const char *devtype) { - _cleanup_free_ char *s = NULL, *d = NULL; int r; assert_return(m, -EINVAL); assert_return(subsystem, -EINVAL); - s = strdup(subsystem); - if (!s) - return -ENOMEM; - - if (devtype) { - d = strdup(devtype); - if (!d) - return -ENOMEM; - } - - r = hashmap_ensure_put(&m->subsystem_filter, NULL, s, d); - if (r < 0) + /* Do not use string_has_ops_free_free or hashmap_put_strdup() here, as this may be called + * multiple times with the same subsystem but different devtypes. */ + r = hashmap_put_strdup_full(&m->subsystem_filter, &trivial_hash_ops_free_free, subsystem, devtype); + if (r <= 0) return r; - TAKE_PTR(s); - TAKE_PTR(d); - m->filter_uptodate = false; - - return 0; + return r; } _public_ int sd_device_monitor_filter_add_match_tag(sd_device_monitor *m, const char *tag) { + int r; + assert_return(m, -EINVAL); assert_return(tag, -EINVAL); - int r = set_put_strdup(&m->tag_filter, tag); - if (r > 0) - m->filter_uptodate = false; + r = set_put_strdup(&m->tag_filter, tag); + if (r <= 0) + return r; + + m->filter_uptodate = false; return r; } @@ -761,8 +752,8 @@ _public_ int sd_device_monitor_filter_remove(sd_device_monitor *m) { assert_return(m, -EINVAL); - m->subsystem_filter = hashmap_free_free_free(m->subsystem_filter); - m->tag_filter = set_free_free(m->tag_filter); + m->subsystem_filter = hashmap_free(m->subsystem_filter); + m->tag_filter = set_free(m->tag_filter); if (setsockopt(m->sock, SOL_SOCKET, SO_DETACH_FILTER, &filter, sizeof(filter)) < 0) return -errno; From 30e2c8c7e87c52045357a0031365a871b66ab449 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 5 Mar 2021 06:39:16 +0900 Subject: [PATCH 4/4] sd-device-enumerator: add comments why trivial_hash_ops_free_free is used Follow-up for a0887abbd8bd9f1a9a975af08e6b4a43960bb3e2. --- src/libsystemd/sd-device/device-enumerator.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c index c98b029af2..2434fb625e 100644 --- a/src/libsystemd/sd-device/device-enumerator.c +++ b/src/libsystemd/sd-device/device-enumerator.c @@ -116,6 +116,8 @@ _public_ int sd_device_enumerator_add_match_sysattr(sd_device_enumerator *enumer else hashmap = &enumerator->nomatch_sysattr; + /* Do not use string_has_ops_free_free or hashmap_put_strdup() here, as this may be called + * multiple times with the same sysattr but different value. */ r = hashmap_put_strdup_full(hashmap, &trivial_hash_ops_free_free, sysattr, value); if (r <= 0) return r; @@ -131,6 +133,8 @@ _public_ int sd_device_enumerator_add_match_property(sd_device_enumerator *enume assert_return(enumerator, -EINVAL); assert_return(property, -EINVAL); + /* Do not use string_has_ops_free_free or hashmap_put_strdup() here, as this may be called + * multiple times with the same property but different value. */ r = hashmap_put_strdup_full(&enumerator->match_property, &trivial_hash_ops_free_free, property, value); if (r <= 0) return r;