From 61c0972dad066749a87d2ecd6cbd883d491518be Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 26 Jan 2021 20:26:33 +0900 Subject: [PATCH] sd-device: use string_hash_ops_free_free --- src/libsystemd/sd-device/sd-device.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/libsystemd/sd-device/sd-device.c b/src/libsystemd/sd-device/sd-device.c index 9b95917574a..d6a21afc4f9 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; } -- 2.47.3