]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: use string_hash_ops_free_free
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 26 Jan 2021 11:26:33 +0000 (20:26 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 26 Jan 2021 11:26:33 +0000 (20:26 +0900)
src/libsystemd/sd-device/sd-device.c

index 9b95917574abc75d3763688ba975b0218605dd61..d6a21afc4f98bb17b62f59820dfe5f95434710cb 100644 (file)
@@ -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;
 }