From dd75bbee4368b336d16759fac2352d9e2e12beaf Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 26 Jan 2021 19:52:56 +0900 Subject: sd-device: use string_hash_ops_free_free --- src/libsystemd/sd-device/sd-device.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index d1aa3282bf..0d9da01daa 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -65,8 +65,8 @@ static sd_device *device_free(sd_device *device) { free(device->properties_strv); free(device->properties_nulstr); - ordered_hashmap_free_free_free(device->properties); - ordered_hashmap_free_free_free(device->properties_db); + ordered_hashmap_free(device->properties); + ordered_hashmap_free(device->properties_db); hashmap_free_free_free(device->sysattr_values); set_free(device->sysattrs); set_free(device->all_tags); @@ -93,7 +93,7 @@ int device_add_property_aux(sd_device *device, const char *_key, const char *_va _cleanup_free_ char *key = NULL, *value = NULL, *old_key = NULL, *old_value = NULL; int r; - r = ordered_hashmap_ensure_allocated(properties, &string_hash_ops); + r = ordered_hashmap_ensure_allocated(properties, &string_hash_ops_free_free); if (r < 0) return r; -- cgit v1.2.3 From 4f1ef7f9e04d976ef39097b955b81ba318850bbc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 26 Jan 2021 19:56:13 +0900 Subject: sd-device: rename variables and use TAKE_PTR() --- src/libsystemd/sd-device/sd-device.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 0d9da01daa..a1dd5bee08 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -78,46 +78,45 @@ static sd_device *device_free(sd_device *device) { DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device, sd_device, device_free); -int device_add_property_aux(sd_device *device, const char *_key, const char *_value, bool db) { +int device_add_property_aux(sd_device *device, const char *key, const char *value, bool db) { OrderedHashmap **properties; assert(device); - assert(_key); + assert(key); if (db) properties = &device->properties_db; else properties = &device->properties; - if (_value) { - _cleanup_free_ char *key = NULL, *value = NULL, *old_key = NULL, *old_value = NULL; + if (value) { + _cleanup_free_ char *new_key = NULL, *new_value = NULL, *old_key = NULL, *old_value = NULL; int r; r = ordered_hashmap_ensure_allocated(properties, &string_hash_ops_free_free); if (r < 0) return r; - key = strdup(_key); - if (!key) + new_key = strdup(key); + if (!new_key) return -ENOMEM; - value = strdup(_value); - if (!value) + new_value = strdup(value); + if (!new_value) return -ENOMEM; old_value = ordered_hashmap_get2(*properties, key, (void**) &old_key); - r = ordered_hashmap_replace(*properties, key, value); + r = ordered_hashmap_replace(*properties, new_key, new_value); if (r < 0) return r; - key = NULL; - value = NULL; + TAKE_PTR(new_key); + TAKE_PTR(new_value); } else { - _cleanup_free_ char *key = NULL; - _cleanup_free_ char *value = NULL; + _cleanup_free_ char *old_key = NULL, *old_value = NULL; - value = ordered_hashmap_remove2(*properties, _key, (void**) &key); + old_value = ordered_hashmap_remove2(*properties, key, (void**) &old_key); } if (!db) { -- cgit v1.2.3 From 327379f5f299d2c7cf174494c430d0643723a309 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 26 Jan 2021 20:07:38 +0900 Subject: sd-device: add a short comment why we simply return negative errno here on failure --- src/libsystemd/sd-device/sd-device.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index a1dd5bee08..9b95917574 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -107,6 +107,7 @@ int device_add_property_aux(sd_device *device, const char *key, const char *valu old_value = ordered_hashmap_get2(*properties, key, (void**) &old_key); + /* ordered_hashmap_replace() does not fail when the hashmap already has the entry. */ r = ordered_hashmap_replace(*properties, new_key, new_value); if (r < 0) return r; -- cgit v1.2.3 From 61c0972dad066749a87d2ecd6cbd883d491518be Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 26 Jan 2021 20:26:33 +0900 Subject: sd-device: use string_hash_ops_free_free --- src/libsystemd/sd-device/sd-device.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 9b95917574..d6a21afc4f 100644 --- a/src/libsystemd/sd-device/sd-device.c +++ b/src/libsystemd/sd-device/sd-device.c @@ -67,7 +67,7 @@ static sd_device *device_free(sd_device *device) { ordered_hashmap_free(device->properties); ordered_hashmap_free(device->properties_db); - hashmap_free_free_free(device->sysattr_values); + hashmap_free(device->sysattr_values); set_free(device->sysattrs); set_free(device->all_tags); set_free(device->current_tags); @@ -1780,30 +1780,28 @@ _public_ int sd_device_get_property_value(sd_device *device, const char *key, co return 0; } -/* replaces the value if it already exists */ -static int device_add_sysattr_value(sd_device *device, const char *_key, char *value) { - _cleanup_free_ char *key = NULL; - _cleanup_free_ char *value_old = NULL; +static int device_add_sysattr_value(sd_device *device, const char *key, char *value) { + _cleanup_free_ char *new_key = NULL, *old_value = NULL; int r; assert(device); - assert(_key); + assert(key); - r = hashmap_ensure_allocated(&device->sysattr_values, &string_hash_ops); - if (r < 0) - return r; + /* This takes the reference of the input value. The input value may be NULL. + * This replaces the value if it already exists. */ - value_old = hashmap_remove2(device->sysattr_values, _key, (void **)&key); - if (!key) { - key = strdup(_key); - if (!key) + old_value = hashmap_remove2(device->sysattr_values, key, (void **) &new_key); + if (!new_key) { + new_key = strdup(key); + if (!new_key) return -ENOMEM; } - r = hashmap_put(device->sysattr_values, key, value); + r = hashmap_ensure_put(&device->sysattr_values, &string_hash_ops_free_free, new_key, value); if (r < 0) return r; - TAKE_PTR(key); + + TAKE_PTR(new_key); return 0; } -- cgit v1.2.3 From ce634c4a4d3b5a3f2ec4c1b2c59e8d36ab9109a4 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 26 Jan 2021 20:35:53 +0900 Subject: sd-device: use size_t for index in the loop --- src/libsystemd/sd-device/device-private.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c index 9070dfbdd1..47ea04921e 100644 --- a/src/libsystemd/sd-device/device-private.c +++ b/src/libsystemd/sd-device/device-private.c @@ -448,7 +448,6 @@ int device_new_from_strv(sd_device **ret, char **strv) { int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) { _cleanup_(sd_device_unrefp) sd_device *device = NULL; const char *major = NULL, *minor = NULL; - unsigned i = 0; int r; assert(ret); @@ -459,11 +458,11 @@ int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) { if (r < 0) return r; - while (i < len) { + for (size_t i = 0; i < len; ) { char *key; const char *end; - key = (char*)&nulstr[i]; + key = (char*) &nulstr[i]; end = memchr(key, '\0', len - i); if (!end) return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL), -- cgit v1.2.3