From: Kefu Chai Date: Fri, 26 Mar 2021 10:34:32 +0000 (+0800) Subject: common/pick_address: use scope_guard for freeifaddrs() X-Git-Tag: v14.2.22~47^2~6 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=412fd5e2406c3294294f228fe2e9532e8cf9fa01;p=ceph.git common/pick_address: use scope_guard for freeifaddrs() for better readability Signed-off-by: Kefu Chai (cherry picked from commit c3c110b5763ac420c4b88f8a545c1c87a71ce59a) --- diff --git a/src/common/pick_address.cc b/src/common/pick_address.cc index e6da4248b50..2a756facaee 100644 --- a/src/common/pick_address.cc +++ b/src/common/pick_address.cc @@ -14,6 +14,7 @@ #include "common/pick_address.h" #include "include/ipaddr.h" +#include "include/scope_guard.h" #include "include/str_list.h" #include "common/ceph_context.h" #ifndef WITH_SEASTAR @@ -187,8 +188,6 @@ static void fill_in_one_address(CephContext *cct, void pick_addresses(CephContext *cct, int needs) { - struct ifaddrs *ifa; - int r = getifaddrs(&ifa); auto public_addr = cct->_conf.get_val("public_addr"); auto public_network = cct->_conf.get_val("public_network"); auto public_network_interface = @@ -198,11 +197,14 @@ void pick_addresses(CephContext *cct, int needs) auto cluster_network_interface = cct->_conf.get_val("cluster_network_interface"); + struct ifaddrs *ifa; + int r = getifaddrs(&ifa); if (r < 0) { string err = cpp_strerror(errno); lderr(cct) << "unable to fetch interfaces and addresses: " << err << dendl; exit(1); } + auto free_ifa = make_scope_guard([ifa] { freeifaddrs(ifa); }); if ((needs & CEPH_PICK_ADDRESS_PUBLIC) && public_addr.is_blank_ip() && !public_network.empty()) { @@ -223,8 +225,6 @@ void pick_addresses(CephContext *cct, int needs) } } } - - freeifaddrs(ifa); } #endif // !WITH_SEASTAR @@ -486,8 +486,8 @@ bool have_local_addr(CephContext *cct, const list& ls, entity_add lderr(cct) << "unable to fetch interfaces and addresses: " << cpp_strerror(errno) << dendl; exit(1); } + auto free_ifa = make_scope_guard([ifa] { freeifaddrs(ifa); }); - bool found = false; for (struct ifaddrs *addrs = ifa; addrs != nullptr; addrs = addrs->ifa_next) { if (addrs->ifa_addr) { entity_addr_t a; @@ -495,16 +495,12 @@ bool have_local_addr(CephContext *cct, const list& ls, entity_add for (auto& p : ls) { if (a.is_same_host(p)) { *match = p; - found = true; - goto out; + return true; } } } } - - out: - freeifaddrs(ifa); - return found; + return false; } int get_iface_numa_node(