]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
resolve: introduce cleanup functions for EtcHostsItemBy{Address,Name}
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 7 Dec 2022 14:30:22 +0000 (23:30 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 13 Dec 2022 11:29:16 +0000 (20:29 +0900)
No functional change, just refactoring and preparation for later
commits.

src/resolve/resolved-etc-hosts.c

index 48d5f106e68e20551709fb02703a5fb936494a00..f41b028b9657c9c4497197d1f1f8b2a4f7555237 100644 (file)
@@ -26,6 +26,8 @@ static EtcHostsItemByAddress *etc_hosts_item_by_address_free(EtcHostsItemByAddre
         return mfree(item);
 }
 
+DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByAddress*, etc_hosts_item_by_address_free);
+
 static EtcHostsItemByName *etc_hosts_item_by_name_free(EtcHostsItemByName *item) {
         if (!item)
                 return NULL;
@@ -35,6 +37,8 @@ static EtcHostsItemByName *etc_hosts_item_by_name_free(EtcHostsItemByName *item)
         return mfree(item);
 }
 
+DEFINE_TRIVIAL_CLEANUP_FUNC(EtcHostsItemByName*, etc_hosts_item_by_name_free);
+
 void etc_hosts_clear(EtcHosts *hosts) {
         assert(hosts);
 
@@ -83,23 +87,21 @@ static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) {
 
                 item = hashmap_get(hosts->by_address, &address);
                 if (!item) {
-                        r = hashmap_ensure_allocated(&hosts->by_address, &in_addr_data_hash_ops);
-                        if (r < 0)
-                                return log_oom();
+                        _cleanup_(etc_hosts_item_by_address_freep) EtcHostsItemByAddress *new_item = NULL;
 
-                        item = new(EtcHostsItemByAddress, 1);
-                        if (!item)
+                        new_item = new(EtcHostsItemByAddress, 1);
+                        if (!new_item)
                                 return log_oom();
 
-                        *item = (EtcHostsItemByAddress) {
+                        *new_item = (EtcHostsItemByAddress) {
                                 .address = address,
                         };
 
-                        r = hashmap_put(hosts->by_address, &item->address, item);
-                        if (r < 0) {
-                                free(item);
+                        r = hashmap_ensure_put(&hosts->by_address, &in_addr_data_hash_ops, &new_item->address, new_item);
+                        if (r < 0)
                                 return log_oom();
-                        }
+
+                        item = TAKE_PTR(new_item);
                 }
         }
 
@@ -141,21 +143,21 @@ static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) {
 
                 bn = hashmap_get(hosts->by_name, name);
                 if (!bn) {
-                        r = hashmap_ensure_allocated(&hosts->by_name, &dns_name_hash_ops);
-                        if (r < 0)
-                                return log_oom();
+                        _cleanup_(etc_hosts_item_by_name_freep) EtcHostsItemByName *new_item = NULL;
 
-                        bn = new0(EtcHostsItemByName, 1);
-                        if (!bn)
+                        new_item = new(EtcHostsItemByName, 1);
+                        if (!new_item)
                                 return log_oom();
 
-                        r = hashmap_put(hosts->by_name, name, bn);
-                        if (r < 0) {
-                                free(bn);
+                        *new_item = (EtcHostsItemByName) {
+                                .name = TAKE_PTR(name),
+                        };
+
+                        r = hashmap_ensure_put(&hosts->by_name, &dns_name_hash_ops, new_item->name, new_item);
+                        if (r < 0)
                                 return log_oom();
-                        }
 
-                        bn->name = TAKE_PTR(name);
+                        bn = TAKE_PTR(new_item);
                 }
 
                 if (!GREEDY_REALLOC(bn->addresses, bn->n_addresses + 1))