diff --git a/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc b/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc index 786332e3902..91f6e6ad946 100644 --- a/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +++ b/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc @@ -450,18 +450,17 @@ grpc_core::UniquePtr GrpcLb::Serverlist::AsText() const { gpr_strvec_init(&entries); for (size_t i = 0; i < serverlist_.size(); ++i) { const GrpcLbServer& server = serverlist_[i]; - char* ipport; + std::string ipport; if (server.drop) { - ipport = gpr_strdup("(drop)"); + ipport = "(drop)"; } else { grpc_resolved_address addr; ParseServer(server, &addr); - grpc_sockaddr_to_string(&ipport, &addr, false); + ipport = grpc_sockaddr_to_string(&addr, false); } char* entry; - gpr_asprintf(&entry, " %" PRIuPTR ": %s token=%s\n", i, ipport, + gpr_asprintf(&entry, " %" PRIuPTR ": %s token=%s\n", i, ipport.c_str(), server.load_balance_token); - gpr_free(ipport); gpr_strvec_add(&entries, entry); } grpc_core::UniquePtr result(gpr_strvec_flatten(&entries, nullptr)); diff --git a/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc b/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc index 92cf17ac751..ab179d30d7d 100644 --- a/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc +++ b/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_channel_secure.cc @@ -53,11 +53,10 @@ RefCountedPtr CreateTargetAuthorityTable( static_cast( gpr_zalloc(sizeof(*target_authority_entries) * addresses.size())); for (size_t i = 0; i < addresses.size(); ++i) { - char* addr_str; - GPR_ASSERT( - grpc_sockaddr_to_string(&addr_str, &addresses[i].address(), true) > 0); - target_authority_entries[i].key = grpc_slice_from_copied_string(addr_str); - gpr_free(addr_str); + std::string addr_str = + grpc_sockaddr_to_string(&addresses[i].address(), true); + target_authority_entries[i].key = + grpc_slice_from_copied_string(addr_str.c_str()); const char* balancer_name = FindGrpclbBalancerNameInChannelArgs(*addresses[i].args()); target_authority_entries[i].value.reset(gpr_strdup(balancer_name)); diff --git a/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc b/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc index 467e7fa76aa..aaf27d76e97 100644 --- a/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +++ b/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc @@ -123,21 +123,12 @@ static void log_address_sorting_list(const grpc_ares_request* r, const ServerAddressList& addresses, const char* input_output_str) { for (size_t i = 0; i < addresses.size(); i++) { - char* addr_str; - if (grpc_sockaddr_to_string(&addr_str, &addresses[i].address(), true)) { - gpr_log( - GPR_INFO, - "(c-ares resolver) request:%p c-ares address sorting: %s[%" PRIuPTR - "]=%s", - r, input_output_str, i, addr_str); - gpr_free(addr_str); - } else { - gpr_log( - GPR_INFO, - "(c-ares resolver) request:%p c-ares address sorting: %s[%" PRIuPTR - "]=", - r, input_output_str, i); - } + std::string addr_str = + grpc_sockaddr_to_string(&addresses[i].address(), true); + gpr_log(GPR_INFO, + "(c-ares resolver) request:%p c-ares address sorting: %s[%" PRIuPTR + "]=%s", + r, input_output_str, i, addr_str.c_str()); } } diff --git a/src/core/ext/filters/client_channel/xds/xds_client.cc b/src/core/ext/filters/client_channel/xds/xds_client.cc index b57aa15725d..a9a5a286670 100644 --- a/src/core/ext/filters/client_channel/xds/xds_client.cc +++ b/src/core/ext/filters/client_channel/xds/xds_client.cc @@ -1115,15 +1115,13 @@ void XdsClient::ChannelState::AdsCallState::AcceptEdsUpdate( locality.name->AsHumanReadableString(), locality.lb_weight, locality.serverlist.size()); for (size_t i = 0; i < locality.serverlist.size(); ++i) { - char* ipport; - grpc_sockaddr_to_string(&ipport, &locality.serverlist[i].address(), - false); + std::string ipport = grpc_sockaddr_to_string( + &locality.serverlist[i].address(), false); gpr_log(GPR_INFO, "[xds_client %p] Priority %" PRIuPTR ", locality %" PRIuPTR " %s, server address %" PRIuPTR ": %s", xds_client(), priority, locality_count, - locality.name->AsHumanReadableString(), i, ipport); - gpr_free(ipport); + locality.name->AsHumanReadableString(), i, ipport.c_str()); } ++locality_count; } diff --git a/src/core/ext/filters/client_channel/xds/xds_client_stats.h b/src/core/ext/filters/client_channel/xds/xds_client_stats.h index b977bd9b7df..2dab43c49bf 100644 --- a/src/core/ext/filters/client_channel/xds/xds_client_stats.h +++ b/src/core/ext/filters/client_channel/xds/xds_client_stats.h @@ -21,10 +21,13 @@ #include +#include + +#include "absl/strings/string_view.h" + #include #include "src/core/lib/gprpp/atomic.h" -#include "src/core/lib/gprpp/map.h" #include "src/core/lib/gprpp/memory.h" #include "src/core/lib/gprpp/ref_counted.h" #include "src/core/lib/gprpp/sync.h" diff --git a/src/core/lib/gprpp/host_port.cc b/src/core/lib/gprpp/host_port.cc index 5170559b1bf..77eb174702e 100644 --- a/src/core/lib/gprpp/host_port.cc +++ b/src/core/lib/gprpp/host_port.cc @@ -20,21 +20,15 @@ #include "src/core/lib/gprpp/host_port.h" -#include - #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" -#include #include -#include - -#include "src/core/lib/gpr/string.h" namespace grpc_core { std::string JoinHostPort(absl::string_view host, int port) { - if (host[0] != '[' && host.rfind(':') != host.npos) { + if (!host.empty() && host[0] != '[' && host.rfind(':') != host.npos) { // IPv6 literals must be enclosed in brackets. return absl::StrFormat("[%s]:%d", host, port); } @@ -42,20 +36,6 @@ std::string JoinHostPort(absl::string_view host, int port) { return absl::StrFormat("%s:%d", host, port); } -int JoinHostPort(grpc_core::UniquePtr* out, const char* host, int port) { - char* tmp; - int ret; - if (host[0] != '[' && strchr(host, ':') != nullptr) { - /* IPv6 literals must be enclosed in brackets. */ - ret = gpr_asprintf(&tmp, "[%s]:%d", host, port); - } else { - /* Ordinary non-bracketed host:port. */ - ret = gpr_asprintf(&tmp, "%s:%d", host, port); - } - out->reset(tmp); - return ret; -} - namespace { bool DoSplitHostPort(absl::string_view name, absl::string_view* host, absl::string_view* port, bool* has_port) { diff --git a/src/core/lib/gprpp/host_port.h b/src/core/lib/gprpp/host_port.h index 1521e2b3831..2d5933508c6 100644 --- a/src/core/lib/gprpp/host_port.h +++ b/src/core/lib/gprpp/host_port.h @@ -21,9 +21,9 @@ #include -#include "absl/strings/string_view.h" +#include -#include "src/core/lib/gprpp/memory.h" +#include "absl/strings/string_view.h" namespace grpc_core { @@ -32,12 +32,6 @@ namespace grpc_core { // like an IPv6 literal. If the host is already bracketed, then additional // brackets will not be added. std::string JoinHostPort(absl::string_view host, int port); -// TODO(roth): Change all callers to use the above variant and then -// remove this one. -/* Usage is similar to gpr_asprintf: returns the number of bytes written - (excluding the final '\0'), and *out points to a string. - In the unlikely event of an error, returns -1 and sets *out to NULL. */ -int JoinHostPort(grpc_core::UniquePtr* out, const char* host, int port); /** Given a name in the form "host:port" or "[ho:st]:port", split into hostname and port number. diff --git a/src/core/lib/iomgr/resolve_address_windows.cc b/src/core/lib/iomgr/resolve_address_windows.cc index 894ea993588..2a07ed130cc 100644 --- a/src/core/lib/iomgr/resolve_address_windows.cc +++ b/src/core/lib/iomgr/resolve_address_windows.cc @@ -113,14 +113,6 @@ static grpc_error* windows_blocking_resolve_address( i++; } - { - for (i = 0; i < (*addresses)->naddrs; i++) { - char* buf; - grpc_sockaddr_to_string(&buf, &(*addresses)->addrs[i], 0); - gpr_free(buf); - } - } - done: if (result) { freeaddrinfo(result); diff --git a/src/core/lib/iomgr/sockaddr_utils.cc b/src/core/lib/iomgr/sockaddr_utils.cc index 2f8ed2225a6..c1447508f3f 100644 --- a/src/core/lib/iomgr/sockaddr_utils.cc +++ b/src/core/lib/iomgr/sockaddr_utils.cc @@ -24,6 +24,8 @@ #include #include +#include "absl/strings/str_format.h" + #include #include #include @@ -150,23 +152,18 @@ void grpc_sockaddr_make_wildcard6(int port, resolved_wild_out->len = static_cast(sizeof(grpc_sockaddr_in6)); } -int grpc_sockaddr_to_string(char** out, - const grpc_resolved_address* resolved_addr, - int normalize) { - const grpc_sockaddr* addr; +std::string grpc_sockaddr_to_string(const grpc_resolved_address* resolved_addr, + bool normalize) { const int save_errno = errno; grpc_resolved_address addr_normalized; - char ntop_buf[GRPC_INET6_ADDRSTRLEN]; - const void* ip = nullptr; - int port = 0; - uint32_t sin6_scope_id = 0; - int ret; - - *out = nullptr; if (normalize && grpc_sockaddr_is_v4mapped(resolved_addr, &addr_normalized)) { resolved_addr = &addr_normalized; } - addr = reinterpret_cast(resolved_addr->addr); + const grpc_sockaddr* addr = + reinterpret_cast(resolved_addr->addr); + const void* ip = nullptr; + int port = 0; + uint32_t sin6_scope_id = 0; if (addr->sa_family == GRPC_AF_INET) { const grpc_sockaddr_in* addr4 = reinterpret_cast(addr); @@ -179,25 +176,24 @@ int grpc_sockaddr_to_string(char** out, port = grpc_ntohs(addr6->sin6_port); sin6_scope_id = addr6->sin6_scope_id; } + char ntop_buf[GRPC_INET6_ADDRSTRLEN]; + std::string out; if (ip != nullptr && grpc_inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != nullptr) { - grpc_core::UniquePtr tmp_out; if (sin6_scope_id != 0) { - char* host_with_scope; - /* Enclose sin6_scope_id with the format defined in RFC 6784 section 2. */ - gpr_asprintf(&host_with_scope, "%s%%25%" PRIu32, ntop_buf, sin6_scope_id); - ret = grpc_core::JoinHostPort(&tmp_out, host_with_scope, port); - gpr_free(host_with_scope); + // Enclose sin6_scope_id with the format defined in RFC 6784 section 2. + std::string host_with_scope = + absl::StrFormat("%s%%25%" PRIu32, ntop_buf, sin6_scope_id); + out = grpc_core::JoinHostPort(host_with_scope, port); } else { - ret = grpc_core::JoinHostPort(&tmp_out, ntop_buf, port); + out = grpc_core::JoinHostPort(ntop_buf, port); } - *out = tmp_out.release(); } else { - ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family); + out = absl::StrFormat("(sockaddr family=%d)", addr->sa_family); } /* This is probably redundant, but we wouldn't want to log the wrong error. */ errno = save_errno; - return ret; + return out; } void grpc_string_to_sockaddr(grpc_resolved_address* out, char* addr, int port) { @@ -226,15 +222,13 @@ char* grpc_sockaddr_to_uri(const grpc_resolved_address* resolved_addr) { if (scheme == nullptr || strcmp("unix", scheme) == 0) { return grpc_sockaddr_to_uri_unix_if_possible(resolved_addr); } - char* path = nullptr; + std::string path = + grpc_sockaddr_to_string(resolved_addr, false /* normalize */); char* uri_str = nullptr; - if (grpc_sockaddr_to_string(&path, resolved_addr, - false /* suppress errors */) && - scheme != nullptr) { - gpr_asprintf(&uri_str, "%s:%s", scheme, path); + if (scheme != nullptr) { + gpr_asprintf(&uri_str, "%s:%s", scheme, path.c_str()); } - gpr_free(path); - return uri_str != nullptr ? uri_str : nullptr; + return uri_str; } const char* grpc_sockaddr_get_uri_scheme( diff --git a/src/core/lib/iomgr/sockaddr_utils.h b/src/core/lib/iomgr/sockaddr_utils.h index 677f953831b..4a844e9d017 100644 --- a/src/core/lib/iomgr/sockaddr_utils.h +++ b/src/core/lib/iomgr/sockaddr_utils.h @@ -21,6 +21,8 @@ #include +#include + #include "src/core/lib/iomgr/resolve_address.h" /* Returns true if addr is an IPv4-mapped IPv6 address within the @@ -56,22 +58,13 @@ int grpc_sockaddr_get_port(const grpc_resolved_address* addr); /* Set IP port number of a sockaddr */ int grpc_sockaddr_set_port(const grpc_resolved_address* addr, int port); -/* Converts a sockaddr into a newly-allocated human-readable string. - - Currently, only the AF_INET and AF_INET6 families are recognized. - If the normalize flag is enabled, ::ffff:0.0.0.0/96 IPv6 addresses are - displayed as plain IPv4. - - Usage is similar to gpr_asprintf: returns the number of bytes written - (excluding the final '\0'), and *out points to a string which must later be - destroyed using gpr_free(). - - In the unlikely event of an error, returns -1 and sets *out to NULL. - The existing value of errno is always preserved. */ -// TODO(roth): Change this to return std::string as part of eliminating -// the old API for JoinHostPort(). -int grpc_sockaddr_to_string(char** out, const grpc_resolved_address* addr, - int normalize); +// Converts a sockaddr into a newly-allocated human-readable string. +// +// Currently, only the AF_INET and AF_INET6 families are recognized. +// If the normalize flag is enabled, ::ffff:0.0.0.0/96 IPv6 addresses are +// displayed as plain IPv4. +std::string grpc_sockaddr_to_string(const grpc_resolved_address* addr, + bool normalize); void grpc_string_to_sockaddr(grpc_resolved_address* out, char* addr, int port); diff --git a/src/core/lib/iomgr/socket_utils_common_posix.cc b/src/core/lib/iomgr/socket_utils_common_posix.cc index f368cb76f62..60432d47162 100644 --- a/src/core/lib/iomgr/socket_utils_common_posix.cc +++ b/src/core/lib/iomgr/socket_utils_common_posix.cc @@ -41,6 +41,8 @@ #include #include +#include + #include #include #include @@ -396,12 +398,10 @@ int grpc_ipv6_loopback_available(void) { static grpc_error* error_for_fd(int fd, const grpc_resolved_address* addr) { if (fd >= 0) return GRPC_ERROR_NONE; - char* addr_str; - grpc_sockaddr_to_string(&addr_str, addr, 0); - grpc_error* err = grpc_error_set_str(GRPC_OS_ERROR(errno, "socket"), - GRPC_ERROR_STR_TARGET_ADDRESS, - grpc_slice_from_copied_string(addr_str)); - gpr_free(addr_str); + std::string addr_str = grpc_sockaddr_to_string(addr, false); + grpc_error* err = grpc_error_set_str( + GRPC_OS_ERROR(errno, "socket"), GRPC_ERROR_STR_TARGET_ADDRESS, + grpc_slice_from_copied_string(addr_str.c_str())); return err; } diff --git a/src/core/lib/iomgr/tcp_client_cfstream.cc b/src/core/lib/iomgr/tcp_client_cfstream.cc index 3e701f8c13e..7461a75e658 100644 --- a/src/core/lib/iomgr/tcp_client_cfstream.cc +++ b/src/core/lib/iomgr/tcp_client_cfstream.cc @@ -143,14 +143,12 @@ static void OnOpen(void* arg, grpc_error* error) { static void ParseResolvedAddress(const grpc_resolved_address* addr, CFStringRef* host, int* port) { - char* host_port; - grpc_sockaddr_to_string(&host_port, addr, 1); + std::string host_port = grpc_sockaddr_to_string(addr, true); std::string host_string; std::string port_string; grpc_core::SplitHostPort(host_port, &host_string, &port_string); *host = CFStringCreateWithCString(NULL, host_string.c_str(), kCFStringEncodingUTF8); - gpr_free(host_port); *port = grpc_sockaddr_get_port(addr); } diff --git a/src/core/lib/iomgr/tcp_server_custom.cc b/src/core/lib/iomgr/tcp_server_custom.cc index 8de12586bbd..49781ada7b1 100644 --- a/src/core/lib/iomgr/tcp_server_custom.cc +++ b/src/core/lib/iomgr/tcp_server_custom.cc @@ -23,6 +23,8 @@ #include #include +#include + #include #include @@ -389,15 +391,9 @@ static grpc_error* tcp_server_add_port(grpc_tcp_server* s, } if (GRPC_TRACE_FLAG_ENABLED(grpc_tcp_trace)) { - char* port_string; - grpc_sockaddr_to_string(&port_string, addr, 0); - const char* str = grpc_error_string(error); - if (port_string) { - gpr_log(GPR_INFO, "SERVER %p add_port %s error=%s", s, port_string, str); - gpr_free(port_string); - } else { - gpr_log(GPR_INFO, "SERVER %p add_port error=%s", s, str); - } + gpr_log(GPR_INFO, "SERVER %p add_port %s error=%s", s, + grpc_sockaddr_to_string(addr, false).c_str(), + grpc_error_string(error)); } family = grpc_sockaddr_get_family(addr); diff --git a/src/core/lib/iomgr/tcp_server_posix.cc b/src/core/lib/iomgr/tcp_server_posix.cc index 25f2e7746bb..25dc7915adb 100644 --- a/src/core/lib/iomgr/tcp_server_posix.cc +++ b/src/core/lib/iomgr/tcp_server_posix.cc @@ -37,6 +37,8 @@ #include #include +#include + #include #include #include @@ -349,7 +351,7 @@ static grpc_error* add_wildcard_addrs_to_server(grpc_tcp_server* s, static grpc_error* clone_port(grpc_tcp_listener* listener, unsigned count) { grpc_tcp_listener* sp = nullptr; - char* addr_str; + std::string addr_str; char* name; grpc_error* err; @@ -368,8 +370,8 @@ static grpc_error* clone_port(grpc_tcp_listener* listener, unsigned count) { true, &port); if (err != GRPC_ERROR_NONE) return err; listener->server->nports++; - grpc_sockaddr_to_string(&addr_str, &listener->addr, 1); - gpr_asprintf(&name, "tcp-server-listener:%s/clone-%d", addr_str, i); + addr_str = grpc_sockaddr_to_string(&listener->addr, true); + gpr_asprintf(&name, "tcp-server-listener:%s/clone-%d", addr_str.c_str(), i); sp = static_cast(gpr_malloc(sizeof(grpc_tcp_listener))); sp->next = listener->next; listener->next = sp; @@ -389,7 +391,6 @@ static grpc_error* clone_port(grpc_tcp_listener* listener, unsigned count) { while (listener->server->tail->next != nullptr) { listener->server->tail = listener->server->tail->next; } - gpr_free(addr_str); gpr_free(name); } diff --git a/src/core/lib/iomgr/tcp_server_utils_posix_common.cc b/src/core/lib/iomgr/tcp_server_utils_posix_common.cc index da18cc39c51..6e9825622c4 100644 --- a/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +++ b/src/core/lib/iomgr/tcp_server_utils_posix_common.cc @@ -29,6 +29,8 @@ #include #include +#include + #include #include #include @@ -83,15 +85,15 @@ static grpc_error* add_socket_to_server(grpc_tcp_server* s, int fd, grpc_tcp_listener** listener) { grpc_tcp_listener* sp = nullptr; int port = -1; - char* addr_str; + std::string addr_str; char* name; grpc_error* err = grpc_tcp_server_prepare_socket(s, fd, addr, s->so_reuseport, &port); if (err == GRPC_ERROR_NONE) { GPR_ASSERT(port > 0); - grpc_sockaddr_to_string(&addr_str, addr, 1); - gpr_asprintf(&name, "tcp-server-listener:%s", addr_str); + addr_str = grpc_sockaddr_to_string(addr, true); + gpr_asprintf(&name, "tcp-server-listener:%s", addr_str.c_str()); gpr_mu_lock(&s->mu); s->nports++; GPR_ASSERT(!s->on_accept_cb && "must add ports before starting server"); @@ -114,7 +116,6 @@ static grpc_error* add_socket_to_server(grpc_tcp_server* s, int fd, sp->sibling = nullptr; GPR_ASSERT(sp->emfd); gpr_mu_unlock(&s->mu); - gpr_free(addr_str); gpr_free(name); } diff --git a/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc b/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc index 7fd86c57ebf..9a6c1694a71 100644 --- a/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc +++ b/src/core/lib/iomgr/tcp_server_utils_posix_ifaddrs.cc @@ -29,6 +29,8 @@ #include #include +#include + #include #include #include @@ -112,7 +114,6 @@ grpc_error* grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s, } for (ifa_it = ifa; ifa_it != nullptr; ifa_it = ifa_it->ifa_next) { grpc_resolved_address addr; - char* addr_str = nullptr; grpc_dualstack_mode dsmode; grpc_tcp_listener* new_sp = nullptr; const char* ifa_name = (ifa_it->ifa_name ? ifa_it->ifa_name : ""); @@ -131,30 +132,27 @@ grpc_error* grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s, err = GRPC_ERROR_CREATE_FROM_STATIC_STRING("Failed to set port"); break; } - if (grpc_sockaddr_to_string(&addr_str, &addr, 0) < 0) { - addr_str = gpr_strdup(""); - } + std::string addr_str = grpc_sockaddr_to_string(&addr, false); gpr_log(GPR_DEBUG, "Adding local addr from interface %s flags 0x%x to server: %s", - ifa_name, ifa_it->ifa_flags, addr_str); + ifa_name, ifa_it->ifa_flags, addr_str.c_str()); /* We could have multiple interfaces with the same address (e.g., bonding), so look for duplicates. */ if (find_listener_with_addr(s, &addr) != nullptr) { - gpr_log(GPR_DEBUG, "Skipping duplicate addr %s on interface %s", addr_str, - ifa_name); - gpr_free(addr_str); + gpr_log(GPR_DEBUG, "Skipping duplicate addr %s on interface %s", + addr_str.c_str(), ifa_name); continue; } if ((err = grpc_tcp_server_add_addr(s, &addr, port_index, fd_index, &dsmode, &new_sp)) != GRPC_ERROR_NONE) { char* err_str = nullptr; grpc_error* root_err; - if (gpr_asprintf(&err_str, "Failed to add listener: %s", addr_str) < 0) { + if (gpr_asprintf(&err_str, "Failed to add listener: %s", + addr_str.c_str()) < 0) { err_str = gpr_strdup("Failed to add listener"); } root_err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(err_str); gpr_free(err_str); - gpr_free(addr_str); err = grpc_error_add_child(root_err, err); break; } else { @@ -166,7 +164,6 @@ grpc_error* grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s, } sp = new_sp; } - gpr_free(addr_str); } freeifaddrs(ifa); if (err != GRPC_ERROR_NONE) { diff --git a/src/core/lib/iomgr/udp_server.cc b/src/core/lib/iomgr/udp_server.cc index ce3531f3378..461f543d675 100644 --- a/src/core/lib/iomgr/udp_server.cc +++ b/src/core/lib/iomgr/udp_server.cc @@ -148,11 +148,9 @@ GrpcUdpListener::GrpcUdpListener(grpc_udp_server* server, int fd, server_(server), orphan_notified_(false), already_shutdown_(false) { - char* addr_str; char* name; - grpc_sockaddr_to_string(&addr_str, addr, 1); - gpr_asprintf(&name, "udp-server-listener:%s", addr_str); - gpr_free(addr_str); + std::string addr_str = grpc_sockaddr_to_string(addr, true); + gpr_asprintf(&name, "udp-server-listener:%s", addr_str.c_str()); emfd_ = grpc_fd_create(fd, name, true); memcpy(&addr_, addr, sizeof(grpc_resolved_address)); GPR_ASSERT(emfd_); @@ -413,10 +411,8 @@ static int prepare_socket(grpc_socket_factory* socket_factory, int fd, } if (bind_socket(socket_factory, fd, addr) < 0) { - char* addr_str; - grpc_sockaddr_to_string(&addr_str, addr, 0); - gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str, strerror(errno)); - gpr_free(addr_str); + std::string addr_str = grpc_sockaddr_to_string(addr, false); + gpr_log(GPR_ERROR, "bind addr=%s: %s", addr_str.c_str(), strerror(errno)); goto error; } @@ -583,10 +579,8 @@ int grpc_udp_server_add_port(grpc_udp_server* s, "Try to have multiple listeners on same port, but SO_REUSEPORT is " "not supported. Only create 1 listener."); } - char* addr_str; - grpc_sockaddr_to_string(&addr_str, addr, 1); - gpr_log(GPR_DEBUG, "add address: %s to server", addr_str); - gpr_free(addr_str); + std::string addr_str = grpc_sockaddr_to_string(addr, true); + gpr_log(GPR_DEBUG, "add address: %s to server", addr_str.c_str()); int allocated_port1 = -1; int allocated_port2 = -1; diff --git a/src/objective-c/tests/CronetTests/CoreCronetEnd2EndTests.mm b/src/objective-c/tests/CronetTests/CoreCronetEnd2EndTests.mm index f2715af6c98..9ed7b7d3122 100644 --- a/src/objective-c/tests/CronetTests/CoreCronetEnd2EndTests.mm +++ b/src/objective-c/tests/CronetTests/CoreCronetEnd2EndTests.mm @@ -52,7 +52,7 @@ #import "../ConfigureCronet.h" struct fullstack_secure_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( @@ -62,7 +62,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( fullstack_secure_fixture_data *ffd = new fullstack_secure_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "127.0.0.1", port); + ffd->localaddr = grpc_core::JoinHostPort("127.0.0.1", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(NULL); @@ -83,7 +83,7 @@ static void cronet_init_client_secure_fullstack(grpc_end2end_test_fixture *f, stream_engine *cronetEngine) { fullstack_secure_fixture_data *ffd = (fullstack_secure_fixture_data *)f->fixture_data; f->client = - grpc_cronet_secure_channel_create(cronetEngine, ffd->localaddr.get(), client_args, NULL); + grpc_cronet_secure_channel_create(cronetEngine, ffd->localaddr.c_str(), client_args, NULL); GPR_ASSERT(f->client != NULL); } @@ -96,7 +96,7 @@ static void chttp2_init_server_secure_fullstack(grpc_end2end_test_fixture *f, } f->server = grpc_server_create(server_args, NULL); grpc_server_register_completion_queue(f->server, f->cq, NULL); - GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr.get(), server_creds)); + GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr.c_str(), server_creds)); grpc_server_credentials_release(server_creds); grpc_server_start(f->server); } diff --git a/src/objective-c/tests/CronetTests/CronetUnitTests.mm b/src/objective-c/tests/CronetTests/CronetUnitTests.mm index 82c995279ec..f3bf73eaa74 100644 --- a/src/objective-c/tests/CronetTests/CronetUnitTests.mm +++ b/src/objective-c/tests/CronetTests/CronetUnitTests.mm @@ -130,11 +130,10 @@ unsigned int parse_h2_length(const char *field) { {{NULL, NULL, NULL, NULL}}}}; int port = grpc_pick_unused_port_or_die(); - grpc_core::UniquePtr addr; - grpc_core::JoinHostPort(&addr, "127.0.0.1", port); + std::string addr = grpc_core::JoinHostPort("127.0.0.1", port); grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL); stream_engine *cronetEngine = [Cronet getGlobalEngine]; - grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr.get(), NULL, NULL); + grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr.c_str(), NULL, NULL); cq_verifier *cqv = cq_verifier_create(cq); grpc_op ops[6]; @@ -261,11 +260,10 @@ unsigned int parse_h2_length(const char *field) { {{NULL, NULL, NULL, NULL}}}}; int port = grpc_pick_unused_port_or_die(); - grpc_core::UniquePtr addr; - grpc_core::JoinHostPort(&addr, "127.0.0.1", port); + std::string addr = grpc_core::JoinHostPort("127.0.0.1", port); grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL); stream_engine *cronetEngine = [Cronet getGlobalEngine]; - grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr.get(), args, NULL); + grpc_channel *client = grpc_cronet_secure_channel_create(cronetEngine, addr.c_str(), args, NULL); cq_verifier *cqv = cq_verifier_create(cq); grpc_op ops[6]; diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/iomgr.pxd.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/iomgr.pxd.pxi index bb934ebb77c..3b64b284aa9 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/iomgr.pxd.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/iomgr.pxd.pxi @@ -13,6 +13,9 @@ # limitations under the License. # distutils: language=c++ +from libcpp cimport bool as bool_t +from libcpp.string cimport string as cppstring + cdef extern from "grpc/impl/codegen/slice.h": struct grpc_slice_buffer: int count @@ -118,8 +121,8 @@ cdef extern from "src/core/lib/iomgr/iomgr_custom.h": cdef extern from "src/core/lib/iomgr/sockaddr_utils.h": int grpc_sockaddr_get_port(const grpc_resolved_address *addr); - int grpc_sockaddr_to_string(char **out, const grpc_resolved_address *addr, - int normalize); + cppstring grpc_sockaddr_to_string(const grpc_resolved_address *addr, + bool_t normalize); void grpc_string_to_sockaddr(grpc_resolved_address *out, char* addr, int port); int grpc_sockaddr_set_port(const grpc_resolved_address *resolved_addr, int port) diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/iomgr.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/iomgr.pyx.pxi index 9274f1c5fdb..3d6bb24f9a1 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/iomgr.pyx.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/iomgr.pyx.pxi @@ -15,6 +15,7 @@ from libc cimport string from libc.stdlib cimport malloc +from libcpp.string cimport string as cppstring cdef grpc_error* grpc_error_none(): return 0 @@ -25,10 +26,10 @@ cdef grpc_error* socket_error(str syscall, str err): return grpc_socket_error(error_bytes) cdef resolved_addr_to_tuple(grpc_resolved_address* address): - cdef char* res_str + cdef cppstring res_str port = grpc_sockaddr_get_port(address) - str_len = grpc_sockaddr_to_string(&res_str, address, 0) - byte_str = _decode(res_str[:str_len]) + res_str = grpc_sockaddr_to_string(address, False) + byte_str = _decode(res_str) if byte_str.endswith(':' + str(port)): byte_str = byte_str[:(0 - len(str(port)) - 1)] byte_str = byte_str.lstrip('[') diff --git a/test/core/bad_ssl/bad_ssl_test.cc b/test/core/bad_ssl/bad_ssl_test.cc index 4c1662d2221..9ef027f2470 100644 --- a/test/core/bad_ssl/bad_ssl_test.cc +++ b/test/core/bad_ssl/bad_ssl_test.cc @@ -145,9 +145,8 @@ int main(int argc, char** argv) { gpr_asprintf(&args[0], "%s/bad_ssl_%s_server%s", root, test, gpr_subprocess_binary_extension()); args[1] = const_cast("--bind"); - grpc_core::UniquePtr joined; - grpc_core::JoinHostPort(&joined, "::", port); - args[2] = joined.get(); + std::string joined = grpc_core::JoinHostPort("::", port); + args[2] = const_cast(joined.c_str()); svr = gpr_subprocess_create(4, (const char**)args); gpr_free(args[0]); diff --git a/test/core/end2end/bad_server_response_test.cc b/test/core/end2end/bad_server_response_test.cc index 3dac90e5225..cdff1079f03 100644 --- a/test/core/end2end/bad_server_response_test.cc +++ b/test/core/end2end/bad_server_response_test.cc @@ -71,7 +71,7 @@ #define SERVER_INCOMING_DATA_LENGTH_LOWER_THRESHOLD (size_t)200 struct rpc_state { - grpc_core::UniquePtr target; + std::string target; grpc_completion_queue* cq; grpc_channel* channel; grpc_call* call; @@ -165,9 +165,9 @@ static void start_rpc(int target_port, grpc_status_code expected_status, state.cq = grpc_completion_queue_create_for_next(nullptr); cqv = cq_verifier_create(state.cq); - grpc_core::JoinHostPort(&state.target, "127.0.0.1", target_port); + state.target = grpc_core::JoinHostPort("127.0.0.1", target_port); state.channel = - grpc_insecure_channel_create(state.target.get(), nullptr, nullptr); + grpc_insecure_channel_create(state.target.c_str(), nullptr, nullptr); grpc_slice host = grpc_slice_from_static_string("localhost"); state.call = grpc_channel_create_call( state.channel, nullptr, GRPC_PROPAGATE_DEFAULTS, state.cq, @@ -231,7 +231,7 @@ static void cleanup_rpc() { } while (ev.type != GRPC_QUEUE_SHUTDOWN); grpc_completion_queue_destroy(state.cq); grpc_channel_destroy(state.channel); - state.target.reset(); + state.target.clear(); } typedef struct { diff --git a/test/core/end2end/connection_refused_test.cc b/test/core/end2end/connection_refused_test.cc index 3bb6d2e23b6..bbbebe901b7 100644 --- a/test/core/end2end/connection_refused_test.cc +++ b/test/core/end2end/connection_refused_test.cc @@ -77,10 +77,9 @@ static void run_test(bool wait_for_ready, bool use_service_config) { /* create a call, channel to a port which will refuse connection */ int port = grpc_pick_unused_port_or_die(); - grpc_core::UniquePtr addr; - grpc_core::JoinHostPort(&addr, "127.0.0.1", port); - gpr_log(GPR_INFO, "server: %s", addr.get()); - chan = grpc_insecure_channel_create(addr.get(), args, nullptr); + std::string addr = grpc_core::JoinHostPort("127.0.0.1", port); + gpr_log(GPR_INFO, "server: %s", addr.c_str()); + chan = grpc_insecure_channel_create(addr.c_str(), args, nullptr); grpc_slice host = grpc_slice_from_static_string("nonexistant"); gpr_timespec deadline = grpc_timeout_seconds_to_deadline(2); call = diff --git a/test/core/end2end/dualstack_socket_test.cc b/test/core/end2end/dualstack_socket_test.cc index 9f0f9816635..affe6d973f8 100644 --- a/test/core/end2end/dualstack_socket_test.cc +++ b/test/core/end2end/dualstack_socket_test.cc @@ -23,6 +23,13 @@ #include +#include + +#include "absl/strings/str_format.h" +#include "absl/strings/str_join.h" +#include "absl/strings/str_split.h" +#include "absl/strings/string_view.h" + #include #include #include @@ -51,8 +58,6 @@ static void drain_cq(grpc_completion_queue* cq) { } while (ev.type != GRPC_QUEUE_SHUTDOWN); } -static void do_nothing(void* /*ignored*/) {} - static void log_resolved_addrs(const char* label, const char* hostname) { grpc_resolved_addresses* res = nullptr; grpc_error* error = grpc_blocking_resolve_address(hostname, "80", &res); @@ -97,8 +102,7 @@ void test_connect(const char* server_host, const char* client_host, int port, picked_port = 1; } - grpc_core::UniquePtr server_hostport; - grpc_core::JoinHostPort(&server_hostport, server_host, port); + std::string server_hostport = grpc_core::JoinHostPort(server_host, port); grpc_metadata_array_init(&initial_metadata_recv); grpc_metadata_array_init(&trailing_metadata_recv); @@ -110,7 +114,7 @@ void test_connect(const char* server_host, const char* client_host, int port, server = grpc_server_create(nullptr, nullptr); grpc_server_register_completion_queue(server, cq, nullptr); GPR_ASSERT((got_port = grpc_server_add_insecure_http2_port( - server, server_hostport.get())) > 0); + server, server_hostport.c_str())) > 0); if (port == 0) { port = got_port; } else { @@ -120,41 +124,25 @@ void test_connect(const char* server_host, const char* client_host, int port, cqv = cq_verifier_create(cq); /* Create client. */ - grpc_core::UniquePtr client_hostport; + std::string client_hostport; if (client_host[0] == 'i') { /* for ipv4:/ipv6: addresses, concatenate the port to each of the parts */ - size_t i; - grpc_slice uri_slice; - grpc_slice_buffer uri_parts; - char** hosts_with_port; - - uri_slice = grpc_slice_new(const_cast(client_host), - strlen(client_host), do_nothing); - grpc_slice_buffer_init(&uri_parts); - grpc_slice_split(uri_slice, ",", &uri_parts); - hosts_with_port = - static_cast(gpr_malloc(sizeof(char*) * uri_parts.count)); - for (i = 0; i < uri_parts.count; i++) { - char* uri_part_str = grpc_slice_to_c_string(uri_parts.slices[i]); - gpr_asprintf(&hosts_with_port[i], "%s:%d", uri_part_str, port); - gpr_free(uri_part_str); - } - client_hostport.reset(gpr_strjoin_sep((const char**)hosts_with_port, - uri_parts.count, ",", nullptr)); - for (i = 0; i < uri_parts.count; i++) { - gpr_free(hosts_with_port[i]); + std::vector uri_parts = + absl::StrSplit(client_host, ",", absl::SkipEmpty()); + std::vector hosts_with_port; + hosts_with_port.reserve(uri_parts.size()); + for (const absl::string_view& uri_part : uri_parts) { + hosts_with_port.push_back(absl::StrFormat("%s:%d", uri_part, port)); } - gpr_free(hosts_with_port); - grpc_slice_buffer_destroy(&uri_parts); - grpc_slice_unref(uri_slice); + client_hostport = absl::StrJoin(hosts_with_port, ","); } else { - grpc_core::JoinHostPort(&client_hostport, client_host, port); + client_hostport = grpc_core::JoinHostPort(client_host, port); } client = - grpc_insecure_channel_create(client_hostport.get(), nullptr, nullptr); + grpc_insecure_channel_create(client_hostport.c_str(), nullptr, nullptr); gpr_log(GPR_INFO, "Testing with server=%s client=%s (expecting %s)", - server_hostport.get(), client_hostport.get(), + server_hostport.c_str(), client_hostport.c_str(), expect_ok ? "success" : "failure"); log_resolved_addrs("server resolved addr", server_host); log_resolved_addrs("client resolved addr", client_host); diff --git a/test/core/end2end/fixtures/h2_census.cc b/test/core/end2end/fixtures/h2_census.cc index afabae10966..2e8429669df 100644 --- a/test/core/end2end/fixtures/h2_census.cc +++ b/test/core/end2end/fixtures/h2_census.cc @@ -36,7 +36,7 @@ #include "test/core/util/test_config.h" struct fullstack_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( @@ -45,7 +45,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( int port = grpc_pick_unused_port_or_die(); fullstack_fixture_data* ffd = new fullstack_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); @@ -68,8 +68,8 @@ void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f, static_cast(f->fixture_data); grpc_arg arg = make_census_enable_arg(); client_args = grpc_channel_args_copy_and_add(client_args, &arg, 1); - f->client = - grpc_insecure_channel_create(ffd->localaddr.get(), client_args, nullptr); + f->client = grpc_insecure_channel_create(ffd->localaddr.c_str(), client_args, + nullptr); GPR_ASSERT(f->client); { grpc_core::ExecCtx exec_ctx; @@ -93,7 +93,7 @@ void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f, } grpc_server_register_completion_queue(f->server, f->cq, nullptr); GPR_ASSERT( - grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.get())); + grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.c_str())); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_compress.cc b/test/core/end2end/fixtures/h2_compress.cc index a0a7c298234..9bc359a8e77 100644 --- a/test/core/end2end/fixtures/h2_compress.cc +++ b/test/core/end2end/fixtures/h2_compress.cc @@ -41,7 +41,7 @@ struct fullstack_compression_fixture_data { grpc_channel_args_destroy(client_args_compression); grpc_channel_args_destroy(server_args_compression); } - grpc_core::UniquePtr localaddr; + std::string localaddr; grpc_channel_args* client_args_compression = nullptr; grpc_channel_args* server_args_compression = nullptr; }; @@ -52,7 +52,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack_compression( int port = grpc_pick_unused_port_or_die(); fullstack_compression_fixture_data* ffd = new fullstack_compression_fixture_data(); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); memset(&f, 0, sizeof(f)); f.fixture_data = ffd; @@ -74,7 +74,7 @@ void chttp2_init_client_fullstack_compression(grpc_end2end_test_fixture* f, grpc_channel_args_set_channel_default_compression_algorithm( client_args, GRPC_COMPRESS_GZIP); f->client = grpc_insecure_channel_create( - ffd->localaddr.get(), ffd->client_args_compression, nullptr); + ffd->localaddr.c_str(), ffd->client_args_compression, nullptr); } void chttp2_init_server_fullstack_compression(grpc_end2end_test_fixture* f, @@ -94,7 +94,7 @@ void chttp2_init_server_fullstack_compression(grpc_end2end_test_fixture* f, f->server = grpc_server_create(ffd->server_args_compression, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); GPR_ASSERT( - grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.get())); + grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.c_str())); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_fakesec.cc b/test/core/end2end/fixtures/h2_fakesec.cc index 6a8ba43e3d6..099bf0d8eef 100644 --- a/test/core/end2end/fixtures/h2_fakesec.cc +++ b/test/core/end2end/fixtures/h2_fakesec.cc @@ -31,7 +31,7 @@ #include "test/core/util/test_config.h" struct fullstack_secure_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( @@ -40,7 +40,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( int port = grpc_pick_unused_port_or_die(); fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); @@ -63,7 +63,7 @@ static void chttp2_init_client_secure_fullstack( grpc_channel_credentials* creds) { fullstack_secure_fixture_data* ffd = static_cast(f->fixture_data); - f->client = grpc_secure_channel_create(creds, ffd->localaddr.get(), + f->client = grpc_secure_channel_create(creds, ffd->localaddr.c_str(), client_args, nullptr); GPR_ASSERT(f->client != nullptr); grpc_channel_credentials_release(creds); @@ -79,8 +79,8 @@ static void chttp2_init_server_secure_fullstack( } f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); - GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr.get(), - server_creds)); + GPR_ASSERT(grpc_server_add_secure_http2_port( + f->server, ffd->localaddr.c_str(), server_creds)); grpc_server_credentials_release(server_creds); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_full+pipe.cc b/test/core/end2end/fixtures/h2_full+pipe.cc index a9807e620de..dd5ae5ba70a 100644 --- a/test/core/end2end/fixtures/h2_full+pipe.cc +++ b/test/core/end2end/fixtures/h2_full+pipe.cc @@ -41,7 +41,7 @@ #include "test/core/util/test_config.h" struct fullstack_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( @@ -51,7 +51,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( fullstack_fixture_data* ffd = new fullstack_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); @@ -64,8 +64,8 @@ void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f, grpc_channel_args* client_args) { fullstack_fixture_data* ffd = static_cast(f->fixture_data); - f->client = - grpc_insecure_channel_create(ffd->localaddr.get(), client_args, nullptr); + f->client = grpc_insecure_channel_create(ffd->localaddr.c_str(), client_args, + nullptr); GPR_ASSERT(f->client); } @@ -79,7 +79,7 @@ void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f, f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); GPR_ASSERT( - grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.get())); + grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.c_str())); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_full+trace.cc b/test/core/end2end/fixtures/h2_full+trace.cc index 64fdca78a4a..68cf39a4c38 100644 --- a/test/core/end2end/fixtures/h2_full+trace.cc +++ b/test/core/end2end/fixtures/h2_full+trace.cc @@ -41,7 +41,7 @@ #include "test/core/util/test_config.h" struct fullstack_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( @@ -51,7 +51,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( fullstack_fixture_data* ffd = new fullstack_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); @@ -64,8 +64,8 @@ void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f, grpc_channel_args* client_args) { fullstack_fixture_data* ffd = static_cast(f->fixture_data); - f->client = - grpc_insecure_channel_create(ffd->localaddr.get(), client_args, nullptr); + f->client = grpc_insecure_channel_create(ffd->localaddr.c_str(), client_args, + nullptr); GPR_ASSERT(f->client); } @@ -79,7 +79,7 @@ void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f, f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); GPR_ASSERT( - grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.get())); + grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.c_str())); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_full+workarounds.cc b/test/core/end2end/fixtures/h2_full+workarounds.cc index d524175925a..13c1f1fa181 100644 --- a/test/core/end2end/fixtures/h2_full+workarounds.cc +++ b/test/core/end2end/fixtures/h2_full+workarounds.cc @@ -40,7 +40,7 @@ static char* workarounds_arg[GRPC_MAX_WORKAROUND_ID] = { const_cast(GRPC_ARG_WORKAROUND_CRONET_COMPRESSION)}; struct fullstack_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( @@ -49,7 +49,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( int port = grpc_pick_unused_port_or_die(); fullstack_fixture_data* ffd = new fullstack_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr); @@ -60,8 +60,8 @@ void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f, grpc_channel_args* client_args) { fullstack_fixture_data* ffd = static_cast(f->fixture_data); - f->client = - grpc_insecure_channel_create(ffd->localaddr.get(), client_args, nullptr); + f->client = grpc_insecure_channel_create(ffd->localaddr.c_str(), client_args, + nullptr); GPR_ASSERT(f->client); } @@ -85,7 +85,7 @@ void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f, f->server = grpc_server_create(server_args_new, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); GPR_ASSERT( - grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.get())); + grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.c_str())); grpc_server_start(f->server); grpc_channel_args_destroy(server_args_new); } diff --git a/test/core/end2end/fixtures/h2_full.cc b/test/core/end2end/fixtures/h2_full.cc index 485c792162c..16c9128e0a8 100644 --- a/test/core/end2end/fixtures/h2_full.cc +++ b/test/core/end2end/fixtures/h2_full.cc @@ -35,7 +35,7 @@ #include "test/core/util/test_config.h" struct fullstack_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( @@ -45,7 +45,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( fullstack_fixture_data* ffd = new fullstack_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); @@ -58,8 +58,8 @@ void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f, grpc_channel_args* client_args) { fullstack_fixture_data* ffd = static_cast(f->fixture_data); - f->client = - grpc_insecure_channel_create(ffd->localaddr.get(), client_args, nullptr); + f->client = grpc_insecure_channel_create(ffd->localaddr.c_str(), client_args, + nullptr); GPR_ASSERT(f->client); } @@ -73,7 +73,7 @@ void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f, f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); GPR_ASSERT( - grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.get())); + grpc_server_add_insecure_http2_port(f->server, ffd->localaddr.c_str())); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_http_proxy.cc b/test/core/end2end/fixtures/h2_http_proxy.cc index e88042d57fb..949356cab2a 100644 --- a/test/core/end2end/fixtures/h2_http_proxy.cc +++ b/test/core/end2end/fixtures/h2_http_proxy.cc @@ -39,7 +39,7 @@ struct fullstack_fixture_data { ~fullstack_fixture_data() { grpc_end2end_http_proxy_destroy(proxy); } - grpc_core::UniquePtr server_addr; + std::string server_addr; grpc_end2end_http_proxy* proxy = nullptr; }; @@ -49,7 +49,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack( memset(&f, 0, sizeof(f)); fullstack_fixture_data* ffd = new fullstack_fixture_data(); const int server_port = grpc_pick_unused_port_or_die(); - grpc_core::JoinHostPort(&ffd->server_addr, "localhost", server_port); + ffd->server_addr = grpc_core::JoinHostPort("localhost", server_port); /* Passing client_args to proxy_create for the case of checking for proxy auth */ @@ -81,8 +81,8 @@ void chttp2_init_client_fullstack(grpc_end2end_test_fixture* f, } gpr_setenv("http_proxy", proxy_uri); gpr_free(proxy_uri); - f->client = grpc_insecure_channel_create(ffd->server_addr.get(), client_args, - nullptr); + f->client = grpc_insecure_channel_create(ffd->server_addr.c_str(), + client_args, nullptr); GPR_ASSERT(f->client); } @@ -96,7 +96,7 @@ void chttp2_init_server_fullstack(grpc_end2end_test_fixture* f, f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); GPR_ASSERT( - grpc_server_add_insecure_http2_port(f->server, ffd->server_addr.get())); + grpc_server_add_insecure_http2_port(f->server, ffd->server_addr.c_str())); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_local_ipv4.cc b/test/core/end2end/fixtures/h2_local_ipv4.cc index 4fbe787174d..f1748078e03 100644 --- a/test/core/end2end/fixtures/h2_local_ipv4.cc +++ b/test/core/end2end/fixtures/h2_local_ipv4.cc @@ -31,10 +31,8 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack_ipv4( grpc_end2end_test_fixture f = grpc_end2end_local_chttp2_create_fixture_fullstack(); int port = grpc_pick_unused_port_or_die(); - grpc_core::JoinHostPort( - &static_cast(f.fixture_data) - ->localaddr, - "127.0.0.1", port); + static_cast(f.fixture_data) + ->localaddr = grpc_core::JoinHostPort("127.0.0.1", port); return f; } diff --git a/test/core/end2end/fixtures/h2_local_ipv6.cc b/test/core/end2end/fixtures/h2_local_ipv6.cc index 3e6cdeec066..b074659de6c 100644 --- a/test/core/end2end/fixtures/h2_local_ipv6.cc +++ b/test/core/end2end/fixtures/h2_local_ipv6.cc @@ -31,10 +31,8 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack_ipv6( grpc_end2end_test_fixture f = grpc_end2end_local_chttp2_create_fixture_fullstack(); int port = grpc_pick_unused_port_or_die(); - grpc_core::JoinHostPort( - &static_cast(f.fixture_data) - ->localaddr, - "[::1]", port); + static_cast(f.fixture_data) + ->localaddr = grpc_core::JoinHostPort("[::1]", port); return f; } diff --git a/test/core/end2end/fixtures/h2_local_uds.cc b/test/core/end2end/fixtures/h2_local_uds.cc index 6b27c41816b..c8db899d524 100644 --- a/test/core/end2end/fixtures/h2_local_uds.cc +++ b/test/core/end2end/fixtures/h2_local_uds.cc @@ -18,6 +18,8 @@ #include +#include "absl/strings/str_format.h" + #include #include "test/core/end2end/end2end_tests.h" @@ -30,10 +32,9 @@ static grpc_end2end_test_fixture chttp2_create_fixture_fullstack_uds( grpc_channel_args* /*client_args*/, grpc_channel_args* /*server_args*/) { grpc_end2end_test_fixture f = grpc_end2end_local_chttp2_create_fixture_fullstack(); - char* out = nullptr; - gpr_asprintf(&out, "unix:/tmp/grpc_fullstack_test.%d.%d", getpid(), unique++); static_cast(f.fixture_data) - ->localaddr.reset(out); + ->localaddr = absl::StrFormat("unix:/tmp/grpc_fullstack_test.%d.%d", + getpid(), unique++); return f; } diff --git a/test/core/end2end/fixtures/h2_oauth2.cc b/test/core/end2end/fixtures/h2_oauth2.cc index 13a9e1c483b..ce37f89e478 100644 --- a/test/core/end2end/fixtures/h2_oauth2.cc +++ b/test/core/end2end/fixtures/h2_oauth2.cc @@ -39,7 +39,7 @@ static const char* client_identity_property_name = "smurf_name"; static const char* client_identity = "Brainy Smurf"; struct fullstack_secure_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static const grpc_metadata* find_metadata(const grpc_metadata* md, @@ -98,7 +98,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( int port = grpc_pick_unused_port_or_die(); fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr); @@ -110,7 +110,7 @@ static void chttp2_init_client_secure_fullstack( grpc_channel_credentials* creds) { fullstack_secure_fixture_data* ffd = static_cast(f->fixture_data); - f->client = grpc_secure_channel_create(creds, ffd->localaddr.get(), + f->client = grpc_secure_channel_create(creds, ffd->localaddr.c_str(), client_args, nullptr); GPR_ASSERT(f->client != nullptr); grpc_channel_credentials_release(creds); @@ -126,8 +126,8 @@ static void chttp2_init_server_secure_fullstack( } f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); - GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr.get(), - server_creds)); + GPR_ASSERT(grpc_server_add_secure_http2_port( + f->server, ffd->localaddr.c_str(), server_creds)); grpc_server_credentials_release(server_creds); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_ssl.cc b/test/core/end2end/fixtures/h2_ssl.cc index 9cb9aaf881a..c44e0699a32 100644 --- a/test/core/end2end/fixtures/h2_ssl.cc +++ b/test/core/end2end/fixtures/h2_ssl.cc @@ -37,7 +37,7 @@ #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key" struct fullstack_secure_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( @@ -47,7 +47,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); @@ -70,7 +70,7 @@ static void chttp2_init_client_secure_fullstack( grpc_channel_credentials* creds) { fullstack_secure_fixture_data* ffd = static_cast(f->fixture_data); - f->client = grpc_secure_channel_create(creds, ffd->localaddr.get(), + f->client = grpc_secure_channel_create(creds, ffd->localaddr.c_str(), client_args, nullptr); GPR_ASSERT(f->client != nullptr); grpc_channel_credentials_release(creds); @@ -86,8 +86,8 @@ static void chttp2_init_server_secure_fullstack( } f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); - GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr.get(), - server_creds)); + GPR_ASSERT(grpc_server_add_secure_http2_port( + f->server, ffd->localaddr.c_str(), server_creds)); grpc_server_credentials_release(server_creds); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_ssl_cred_reload.cc b/test/core/end2end/fixtures/h2_ssl_cred_reload.cc index 589f2fd3d92..96f796d0b27 100644 --- a/test/core/end2end/fixtures/h2_ssl_cred_reload.cc +++ b/test/core/end2end/fixtures/h2_ssl_cred_reload.cc @@ -37,7 +37,7 @@ #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key" struct fullstack_secure_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; bool server_credential_reloaded = false; }; @@ -82,7 +82,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( int port = grpc_pick_unused_port_or_die(); fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); @@ -105,7 +105,7 @@ static void chttp2_init_client_secure_fullstack( grpc_channel_credentials* creds) { fullstack_secure_fixture_data* ffd = static_cast(f->fixture_data); - f->client = grpc_secure_channel_create(creds, ffd->localaddr.get(), + f->client = grpc_secure_channel_create(creds, ffd->localaddr.c_str(), client_args, nullptr); GPR_ASSERT(f->client != nullptr); grpc_channel_credentials_release(creds); @@ -122,8 +122,8 @@ static void chttp2_init_server_secure_fullstack( ffd->server_credential_reloaded = false; f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); - GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr.get(), - server_creds)); + GPR_ASSERT(grpc_server_add_secure_http2_port( + f->server, ffd->localaddr.c_str(), server_creds)); grpc_server_credentials_release(server_creds); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/h2_tls.cc b/test/core/end2end/fixtures/h2_tls.cc index 1a79bc96014..bacbf7efff4 100644 --- a/test/core/end2end/fixtures/h2_tls.cc +++ b/test/core/end2end/fixtures/h2_tls.cc @@ -51,7 +51,7 @@ struct fullstack_secure_fixture_data { thd_list[ind].Join(); } } - grpc_core::UniquePtr localaddr; + std::string localaddr; ThreadList thd_list; }; @@ -61,7 +61,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( int port = grpc_pick_unused_port_or_die(); fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr); @@ -82,7 +82,7 @@ static void chttp2_init_client_secure_fullstack( grpc_channel_credentials* creds) { fullstack_secure_fixture_data* ffd = static_cast(f->fixture_data); - f->client = grpc_secure_channel_create(creds, ffd->localaddr.get(), + f->client = grpc_secure_channel_create(creds, ffd->localaddr.c_str(), client_args, nullptr); GPR_ASSERT(f->client != nullptr); grpc_channel_credentials_release(creds); @@ -98,8 +98,8 @@ static void chttp2_init_server_secure_fullstack( } f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); - GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr.get(), - server_creds)); + GPR_ASSERT(grpc_server_add_secure_http2_port( + f->server, ffd->localaddr.c_str(), server_creds)); grpc_server_credentials_release(server_creds); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/http_proxy_fixture.cc b/test/core/end2end/fixtures/http_proxy_fixture.cc index de8cec386d4..b827f58e795 100644 --- a/test/core/end2end/fixtures/http_proxy_fixture.cc +++ b/test/core/end2end/fixtures/http_proxy_fixture.cc @@ -61,7 +61,7 @@ struct grpc_end2end_http_proxy { gpr_ref_init(&users, 1); combiner = grpc_combiner_create(); } - grpc_core::UniquePtr proxy_name; + std::string proxy_name; grpc_core::Thread thd; grpc_tcp_server* server; grpc_channel_args* channel_args; @@ -612,8 +612,8 @@ grpc_end2end_http_proxy* grpc_end2end_http_proxy_create( grpc_end2end_http_proxy* proxy = new grpc_end2end_http_proxy(); // Construct proxy address. const int proxy_port = grpc_pick_unused_port_or_die(); - grpc_core::JoinHostPort(&proxy->proxy_name, "localhost", proxy_port); - gpr_log(GPR_INFO, "Proxy address: %s", proxy->proxy_name.get()); + proxy->proxy_name = grpc_core::JoinHostPort("localhost", proxy_port); + gpr_log(GPR_INFO, "Proxy address: %s", proxy->proxy_name.c_str()); // Create TCP server. proxy->channel_args = grpc_channel_args_copy(args); grpc_error* error = @@ -663,5 +663,5 @@ void grpc_end2end_http_proxy_destroy(grpc_end2end_http_proxy* proxy) { const char* grpc_end2end_http_proxy_get_proxy_name( grpc_end2end_http_proxy* proxy) { - return proxy->proxy_name.get(); + return proxy->proxy_name.c_str(); } diff --git a/test/core/end2end/fixtures/local_util.cc b/test/core/end2end/fixtures/local_util.cc index fadf80caf4f..73679dd5aeb 100644 --- a/test/core/end2end/fixtures/local_util.cc +++ b/test/core/end2end/fixtures/local_util.cc @@ -50,7 +50,7 @@ void grpc_end2end_local_chttp2_init_client_fullstack( grpc_channel_credentials* creds = grpc_local_credentials_create(type); grpc_end2end_local_fullstack_fixture_data* ffd = static_cast(f->fixture_data); - f->client = grpc_secure_channel_create(creds, ffd->localaddr.get(), + f->client = grpc_secure_channel_create(creds, ffd->localaddr.c_str(), client_args, nullptr); GPR_ASSERT(f->client != nullptr); grpc_channel_credentials_release(creds); @@ -98,8 +98,8 @@ void grpc_end2end_local_chttp2_init_server_fullstack( nullptr}; grpc_server_credentials_set_auth_metadata_processor(creds, processor); } - GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr.get(), - creds)); + GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, + ffd->localaddr.c_str(), creds)); grpc_server_credentials_release(creds); grpc_server_start(f->server); } diff --git a/test/core/end2end/fixtures/local_util.h b/test/core/end2end/fixtures/local_util.h index df204d2fab2..9e7a3345d0c 100644 --- a/test/core/end2end/fixtures/local_util.h +++ b/test/core/end2end/fixtures/local_util.h @@ -23,7 +23,7 @@ #include "src/core/lib/surface/channel.h" struct grpc_end2end_local_fullstack_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; /* Utility functions shared by h2_local tests. */ diff --git a/test/core/end2end/fixtures/proxy.cc b/test/core/end2end/fixtures/proxy.cc index ff6ee12ca77..919d6321ed8 100644 --- a/test/core/end2end/fixtures/proxy.cc +++ b/test/core/end2end/fixtures/proxy.cc @@ -41,8 +41,8 @@ struct grpc_end2end_proxy { memset(&new_call_metadata, 0, sizeof(new_call_metadata)); } grpc_core::Thread thd; - grpc_core::UniquePtr proxy_port; - grpc_core::UniquePtr server_port; + std::string proxy_port; + std::string server_port; grpc_completion_queue* cq; grpc_server* server; grpc_channel* client; @@ -91,15 +91,15 @@ grpc_end2end_proxy* grpc_end2end_proxy_create(const grpc_end2end_proxy_def* def, grpc_end2end_proxy* proxy = new grpc_end2end_proxy(); - grpc_core::JoinHostPort(&proxy->proxy_port, "localhost", proxy_port); - grpc_core::JoinHostPort(&proxy->server_port, "localhost", server_port); + proxy->proxy_port = grpc_core::JoinHostPort("localhost", proxy_port); + proxy->server_port = grpc_core::JoinHostPort("localhost", server_port); - gpr_log(GPR_DEBUG, "PROXY ADDR:%s BACKEND:%s", proxy->proxy_port.get(), - proxy->server_port.get()); + gpr_log(GPR_DEBUG, "PROXY ADDR:%s BACKEND:%s", proxy->proxy_port.c_str(), + proxy->server_port.c_str()); proxy->cq = grpc_completion_queue_create_for_next(nullptr); - proxy->server = def->create_server(proxy->proxy_port.get(), server_args); - proxy->client = def->create_client(proxy->server_port.get(), client_args); + proxy->server = def->create_server(proxy->proxy_port.c_str(), server_args); + proxy->client = def->create_client(proxy->server_port.c_str(), client_args); grpc_server_register_completion_queue(proxy->server, proxy->cq, nullptr); grpc_server_start(proxy->server); @@ -440,9 +440,9 @@ static void thread_main(void* arg) { } const char* grpc_end2end_proxy_get_client_target(grpc_end2end_proxy* proxy) { - return proxy->proxy_port.get(); + return proxy->proxy_port.c_str(); } const char* grpc_end2end_proxy_get_server_port(grpc_end2end_proxy* proxy) { - return proxy->server_port.get(); + return proxy->server_port.c_str(); } diff --git a/test/core/end2end/h2_ssl_cert_test.cc b/test/core/end2end/h2_ssl_cert_test.cc index a555c5c4349..21c3d288a41 100644 --- a/test/core/end2end/h2_ssl_cert_test.cc +++ b/test/core/end2end/h2_ssl_cert_test.cc @@ -48,7 +48,7 @@ namespace grpc { namespace testing { struct fullstack_secure_fixture_data { - grpc_core::UniquePtr localaddr; + std::string localaddr; }; static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( @@ -58,7 +58,7 @@ static grpc_end2end_test_fixture chttp2_create_fixture_secure_fullstack( fullstack_secure_fixture_data* ffd = new fullstack_secure_fixture_data(); memset(&f, 0, sizeof(f)); - grpc_core::JoinHostPort(&ffd->localaddr, "localhost", port); + ffd->localaddr = grpc_core::JoinHostPort("localhost", port); f.fixture_data = ffd; f.cq = grpc_completion_queue_create_for_next(nullptr); @@ -79,7 +79,7 @@ static void chttp2_init_client_secure_fullstack( grpc_channel_credentials* creds) { fullstack_secure_fixture_data* ffd = static_cast(f->fixture_data); - f->client = grpc_secure_channel_create(creds, ffd->localaddr.get(), + f->client = grpc_secure_channel_create(creds, ffd->localaddr.c_str(), client_args, nullptr); GPR_ASSERT(f->client != nullptr); grpc_channel_credentials_release(creds); @@ -95,8 +95,8 @@ static void chttp2_init_server_secure_fullstack( } f->server = grpc_server_create(server_args, nullptr); grpc_server_register_completion_queue(f->server, f->cq, nullptr); - GPR_ASSERT(grpc_server_add_secure_http2_port(f->server, ffd->localaddr.get(), - server_creds)); + GPR_ASSERT(grpc_server_add_secure_http2_port( + f->server, ffd->localaddr.c_str(), server_creds)); grpc_server_credentials_release(server_creds); grpc_server_start(f->server); } diff --git a/test/core/end2end/h2_ssl_session_reuse_test.cc b/test/core/end2end/h2_ssl_session_reuse_test.cc index 2683f3df175..a209d8f62f8 100644 --- a/test/core/end2end/h2_ssl_session_reuse_test.cc +++ b/test/core/end2end/h2_ssl_session_reuse_test.cc @@ -48,7 +48,7 @@ void* tag(intptr_t t) { return (void*)t; } gpr_timespec five_seconds_time() { return grpc_timeout_seconds_to_deadline(5); } -grpc_server* server_create(grpc_completion_queue* cq, char* server_addr) { +grpc_server* server_create(grpc_completion_queue* cq, const char* server_addr) { grpc_slice ca_slice, cert_slice, key_slice; GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", grpc_load_file(CA_CERT_PATH, 1, &ca_slice))); @@ -80,7 +80,8 @@ grpc_server* server_create(grpc_completion_queue* cq, char* server_addr) { return server; } -grpc_channel* client_create(char* server_addr, grpc_ssl_session_cache* cache) { +grpc_channel* client_create(const char* server_addr, + grpc_ssl_session_cache* cache) { grpc_slice ca_slice, cert_slice, key_slice; GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", grpc_load_file(CA_CERT_PATH, 1, &ca_slice))); @@ -126,7 +127,7 @@ grpc_channel* client_create(char* server_addr, grpc_ssl_session_cache* cache) { } void do_round_trip(grpc_completion_queue* cq, grpc_server* server, - char* server_addr, grpc_ssl_session_cache* cache, + const char* server_addr, grpc_ssl_session_cache* cache, bool expect_session_reuse) { grpc_channel* client = client_create(server_addr, cache); @@ -249,17 +250,16 @@ void drain_cq(grpc_completion_queue* cq) { TEST(H2SessionReuseTest, SingleReuse) { int port = grpc_pick_unused_port_or_die(); - grpc_core::UniquePtr server_addr; - grpc_core::JoinHostPort(&server_addr, "localhost", port); + std::string server_addr = grpc_core::JoinHostPort("localhost", port); grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); grpc_ssl_session_cache* cache = grpc_ssl_session_cache_create_lru(16); - grpc_server* server = server_create(cq, server_addr.get()); + grpc_server* server = server_create(cq, server_addr.c_str()); - do_round_trip(cq, server, server_addr.get(), cache, false); - do_round_trip(cq, server, server_addr.get(), cache, true); - do_round_trip(cq, server, server_addr.get(), cache, true); + do_round_trip(cq, server, server_addr.c_str(), cache, false); + do_round_trip(cq, server, server_addr.c_str(), cache, true); + do_round_trip(cq, server, server_addr.c_str(), cache, true); grpc_ssl_session_cache_destroy(cache); diff --git a/test/core/end2end/invalid_call_argument_test.cc b/test/core/end2end/invalid_call_argument_test.cc index 5f920fad638..75725a6ccef 100644 --- a/test/core/end2end/invalid_call_argument_test.cc +++ b/test/core/end2end/invalid_call_argument_test.cc @@ -77,13 +77,13 @@ static void prepare_test(int is_client) { } else { g_state.server = grpc_server_create(nullptr, nullptr); grpc_server_register_completion_queue(g_state.server, g_state.cq, nullptr); - grpc_core::UniquePtr server_hostport; - grpc_core::JoinHostPort(&server_hostport, "0.0.0.0", port); - grpc_server_add_insecure_http2_port(g_state.server, server_hostport.get()); + std::string server_hostport = grpc_core::JoinHostPort("0.0.0.0", port); + grpc_server_add_insecure_http2_port(g_state.server, + server_hostport.c_str()); grpc_server_start(g_state.server); - grpc_core::JoinHostPort(&server_hostport, "localhost", port); + server_hostport = grpc_core::JoinHostPort("localhost", port); g_state.chan = - grpc_insecure_channel_create(server_hostport.get(), nullptr, nullptr); + grpc_insecure_channel_create(server_hostport.c_str(), nullptr, nullptr); grpc_slice host = grpc_slice_from_static_string("bar"); g_state.call = grpc_channel_create_call( g_state.chan, nullptr, GRPC_PROPAGATE_DEFAULTS, g_state.cq, diff --git a/test/core/fling/fling_stream_test.cc b/test/core/fling/fling_stream_test.cc index edd5616c824..b4565eced6f 100644 --- a/test/core/fling/fling_stream_test.cc +++ b/test/core/fling/fling_stream_test.cc @@ -46,9 +46,8 @@ int main(int /*argc*/, char** argv) { gpr_asprintf(&args[0], "%s/fling_server%s", root, gpr_subprocess_binary_extension()); args[1] = const_cast("--bind"); - grpc_core::UniquePtr joined; - grpc_core::JoinHostPort(&joined, "::", port); - args[2] = joined.get(); + std::string joined = grpc_core::JoinHostPort("::", port); + args[2] = const_cast(joined.c_str()); args[3] = const_cast("--no-secure"); svr = gpr_subprocess_create(4, (const char**)args); gpr_free(args[0]); @@ -57,8 +56,8 @@ int main(int /*argc*/, char** argv) { gpr_asprintf(&args[0], "%s/fling_client%s", root, gpr_subprocess_binary_extension()); args[1] = const_cast("--target"); - grpc_core::JoinHostPort(&joined, "127.0.0.1", port); - args[2] = joined.get(); + joined = grpc_core::JoinHostPort("127.0.0.1", port); + args[2] = const_cast(joined.c_str()); args[3] = const_cast("--scenario=ping-pong-stream"); args[4] = const_cast("--no-secure"); args[5] = nullptr; diff --git a/test/core/fling/fling_test.cc b/test/core/fling/fling_test.cc index 00d399c4196..717bccd8544 100644 --- a/test/core/fling/fling_test.cc +++ b/test/core/fling/fling_test.cc @@ -47,9 +47,8 @@ int main(int /*argc*/, const char** argv) { gpr_asprintf(&args[0], "%s/fling_server%s", root, gpr_subprocess_binary_extension()); args[1] = const_cast("--bind"); - grpc_core::UniquePtr joined; - grpc_core::JoinHostPort(&joined, "::", port); - args[2] = joined.get(); + std::string joined = grpc_core::JoinHostPort("::", port); + args[2] = const_cast(joined.c_str()); args[3] = const_cast("--no-secure"); svr = gpr_subprocess_create(4, (const char**)args); gpr_free(args[0]); @@ -58,8 +57,8 @@ int main(int /*argc*/, const char** argv) { gpr_asprintf(&args[0], "%s/fling_client%s", root, gpr_subprocess_binary_extension()); args[1] = const_cast("--target"); - grpc_core::JoinHostPort(&joined, "127.0.0.1", port); - args[2] = joined.get(); + joined = grpc_core::JoinHostPort("127.0.0.1", port); + args[2] = const_cast(joined.c_str()); args[3] = const_cast("--scenario=ping-pong-request"); args[4] = const_cast("--no-secure"); args[5] = nullptr; diff --git a/test/core/fling/server.cc b/test/core/fling/server.cc index 878bff3a9fd..4085e28ed22 100644 --- a/test/core/fling/server.cc +++ b/test/core/fling/server.cc @@ -172,7 +172,7 @@ static void sigint_handler(int /*x*/) { _exit(0); } int main(int argc, char** argv) { grpc_event ev; call_state* s; - grpc_core::UniquePtr addr_buf; + std::string addr_buf; gpr_cmdline* cl; grpc_completion_queue* shutdown_cq; int shutdown_started = 0; @@ -199,8 +199,8 @@ int main(int argc, char** argv) { gpr_cmdline_destroy(cl); if (addr == nullptr) { - grpc_core::JoinHostPort(&addr_buf, "::", grpc_pick_unused_port_or_die()); - addr = addr_buf.get(); + addr_buf = grpc_core::JoinHostPort("::", grpc_pick_unused_port_or_die()); + addr = addr_buf.c_str(); } gpr_log(GPR_INFO, "creating server on: %s", addr); @@ -221,7 +221,7 @@ int main(int argc, char** argv) { grpc_server_start(server); addr = nullptr; - addr_buf.reset(); + addr_buf.clear(); grpc_call_details_init(&call_details); diff --git a/test/core/gprpp/host_port_test.cc b/test/core/gprpp/host_port_test.cc index 54914808545..0cff9975f1d 100644 --- a/test/core/gprpp/host_port_test.cc +++ b/test/core/gprpp/host_port_test.cc @@ -26,12 +26,8 @@ static void join_host_port_expect(const char* host, int port, const char* expected) { - grpc_core::UniquePtr buf; - int len; - len = grpc_core::JoinHostPort(&buf, host, port); - GPR_ASSERT(len >= 0); - GPR_ASSERT(strlen(expected) == static_cast(len)); - GPR_ASSERT(strcmp(expected, buf.get()) == 0); + std::string actual = grpc_core::JoinHostPort(host, port); + GPR_ASSERT(actual == expected); } static void test_join_host_port(void) { diff --git a/test/core/iomgr/sockaddr_utils_test.cc b/test/core/iomgr/sockaddr_utils_test.cc index 250d36a74a6..bd92426bfc2 100644 --- a/test/core/iomgr/sockaddr_utils_test.cc +++ b/test/core/iomgr/sockaddr_utils_test.cc @@ -175,15 +175,9 @@ static void test_sockaddr_is_wildcard(void) { static void expect_sockaddr_str(const char* expected, grpc_resolved_address* addr, int normalize) { - int result; - char* str; gpr_log(GPR_INFO, " expect_sockaddr_str(%s)", expected); - result = grpc_sockaddr_to_string(&str, addr, normalize); - GPR_ASSERT(str != nullptr); - GPR_ASSERT(result >= 0); - GPR_ASSERT((size_t)result == strlen(str)); - GPR_ASSERT(strcmp(expected, str) == 0); - gpr_free(str); + std::string actual = grpc_sockaddr_to_string(addr, normalize); + GPR_ASSERT(actual == expected); } static void expect_sockaddr_uri(const char* expected, diff --git a/test/core/iomgr/tcp_server_posix_test.cc b/test/core/iomgr/tcp_server_posix_test.cc index b2d64f4f42a..8594344ae97 100644 --- a/test/core/iomgr/tcp_server_posix_test.cc +++ b/test/core/iomgr/tcp_server_posix_test.cc @@ -32,6 +32,8 @@ #include #include +#include + #include #include #include @@ -133,15 +135,10 @@ static void server_weak_ref_set(server_weak_ref* weak_ref, } static void test_addr_init_str(test_addr* addr) { - char* str = nullptr; - if (grpc_sockaddr_to_string(&str, &addr->addr, 0) != -1) { - size_t str_len; - memcpy(addr->str, str, (str_len = strnlen(str, sizeof(addr->str) - 1))); - addr->str[str_len] = '\0'; - gpr_free(str); - } else { - addr->str[0] = '\0'; - } + std::string str = grpc_sockaddr_to_string(&addr->addr, false); + size_t str_len = std::min(str.size(), sizeof(addr->str) - 1); + memcpy(addr->str, str.c_str(), str_len); + addr->str[str_len] = '\0'; } static void on_connect(void* /*arg*/, grpc_endpoint* tcp, diff --git a/test/core/surface/num_external_connectivity_watchers_test.cc b/test/core/surface/num_external_connectivity_watchers_test.cc index 541e9abda0f..1b2cce86819 100644 --- a/test/core/surface/num_external_connectivity_watchers_test.cc +++ b/test/core/surface/num_external_connectivity_watchers_test.cc @@ -69,11 +69,11 @@ static void channel_idle_poll_for_timeout(grpc_channel* channel, static void run_timeouts_test(const test_fixture* fixture) { gpr_log(GPR_INFO, "TEST: %s", fixture->name); - grpc_core::UniquePtr addr; grpc_init(); - grpc_core::JoinHostPort(&addr, "localhost", grpc_pick_unused_port_or_die()); + std::string addr = + grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die()); - grpc_channel* channel = fixture->create_channel(addr.get()); + grpc_channel* channel = fixture->create_channel(addr.c_str()); grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); /* start 1 watcher and then let it time out */ @@ -120,11 +120,11 @@ static void run_channel_shutdown_before_timeout_test( const test_fixture* fixture) { gpr_log(GPR_INFO, "TEST: %s", fixture->name); - grpc_core::UniquePtr addr; grpc_init(); - grpc_core::JoinHostPort(&addr, "localhost", grpc_pick_unused_port_or_die()); + std::string addr = + grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die()); - grpc_channel* channel = fixture->create_channel(addr.get()); + grpc_channel* channel = fixture->create_channel(addr.c_str()); grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); /* start 1 watcher and then shut down the channel before the timer goes off */ diff --git a/test/core/surface/sequential_connectivity_test.cc b/test/core/surface/sequential_connectivity_test.cc index 8f7a46bfe38..ed20017d708 100644 --- a/test/core/surface/sequential_connectivity_test.cc +++ b/test/core/surface/sequential_connectivity_test.cc @@ -66,11 +66,11 @@ static void run_test(const test_fixture* fixture) { grpc_init(); - grpc_core::UniquePtr addr; - grpc_core::JoinHostPort(&addr, "localhost", grpc_pick_unused_port_or_die()); + std::string addr = + grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die()); grpc_server* server = grpc_server_create(nullptr, nullptr); - fixture->add_server_port(server, addr.get()); + fixture->add_server_port(server, addr.c_str()); grpc_completion_queue* server_cq = grpc_completion_queue_create_for_next(nullptr); grpc_server_register_completion_queue(server, server_cq, nullptr); @@ -83,7 +83,7 @@ static void run_test(const test_fixture* fixture) { grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); grpc_channel* channels[NUM_CONNECTIONS]; for (size_t i = 0; i < NUM_CONNECTIONS; i++) { - channels[i] = fixture->create_channel(addr.get()); + channels[i] = fixture->create_channel(addr.c_str()); gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline(30); grpc_connectivity_state state; diff --git a/test/core/surface/server_chttp2_test.cc b/test/core/surface/server_chttp2_test.cc index a88362e13b0..3399b07a1c0 100644 --- a/test/core/surface/server_chttp2_test.cc +++ b/test/core/surface/server_chttp2_test.cc @@ -47,15 +47,15 @@ void test_add_same_port_twice() { grpc_channel_args args = {1, &a}; int port = grpc_pick_unused_port_or_die(); - grpc_core::UniquePtr addr; grpc_completion_queue* cq = grpc_completion_queue_create_for_pluck(nullptr); grpc_server* server = grpc_server_create(&args, nullptr); grpc_server_credentials* fake_creds = grpc_fake_transport_security_server_credentials_create(); - grpc_core::JoinHostPort(&addr, "localhost", port); - GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr.get(), fake_creds)); + std::string addr = grpc_core::JoinHostPort("localhost", port); GPR_ASSERT( - grpc_server_add_secure_http2_port(server, addr.get(), fake_creds) == 0); + grpc_server_add_secure_http2_port(server, addr.c_str(), fake_creds)); + GPR_ASSERT( + grpc_server_add_secure_http2_port(server, addr.c_str(), fake_creds) == 0); grpc_server_credentials_release(fake_creds); grpc_server_shutdown_and_notify(server, cq, nullptr); diff --git a/test/core/surface/server_test.cc b/test/core/surface/server_test.cc index 185a6757743..e53d7eb90fa 100644 --- a/test/core/surface/server_test.cc +++ b/test/core/surface/server_test.cc @@ -106,19 +106,18 @@ void test_bind_server_twice(void) { void test_bind_server_to_addr(const char* host, bool secure) { int port = grpc_pick_unused_port_or_die(); - grpc_core::UniquePtr addr; - grpc_core::JoinHostPort(&addr, host, port); - gpr_log(GPR_INFO, "Test bind to %s", addr.get()); + std::string addr = grpc_core::JoinHostPort(host, port); + gpr_log(GPR_INFO, "Test bind to %s", addr.c_str()); grpc_server* server = grpc_server_create(nullptr, nullptr); if (secure) { grpc_server_credentials* fake_creds = grpc_fake_transport_security_server_credentials_create(); GPR_ASSERT( - grpc_server_add_secure_http2_port(server, addr.get(), fake_creds)); + grpc_server_add_secure_http2_port(server, addr.c_str(), fake_creds)); grpc_server_credentials_release(fake_creds); } else { - GPR_ASSERT(grpc_server_add_insecure_http2_port(server, addr.get())); + GPR_ASSERT(grpc_server_add_insecure_http2_port(server, addr.c_str())); } grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); grpc_server_register_completion_queue(server, cq, nullptr); diff --git a/test/core/tsi/alts/handshaker/alts_concurrent_connectivity_test.cc b/test/core/tsi/alts/handshaker/alts_concurrent_connectivity_test.cc index 18cfa0dc4b3..521cabdf932 100644 --- a/test/core/tsi/alts/handshaker/alts_concurrent_connectivity_test.cc +++ b/test/core/tsi/alts/handshaker/alts_concurrent_connectivity_test.cc @@ -103,7 +103,7 @@ class FakeHandshakeServer { public: FakeHandshakeServer(bool check_num_concurrent_rpcs) { int port = grpc_pick_unused_port_or_die(); - grpc_core::JoinHostPort(&address_, "localhost", port); + address_ = grpc_core::JoinHostPort("localhost", port); if (check_num_concurrent_rpcs) { service_ = grpc::gcp:: CreateFakeHandshakerService(kFakeHandshakeServerMaxConcurrentStreams /* expected max concurrent rpcs */); @@ -112,22 +112,24 @@ class FakeHandshakeServer { 0 /* expected max concurrent rpcs unset */); } grpc::ServerBuilder builder; - builder.AddListeningPort(address_.get(), grpc::InsecureServerCredentials()); + builder.AddListeningPort(address_.c_str(), + grpc::InsecureServerCredentials()); builder.RegisterService(service_.get()); // TODO(apolcyn): when removing the global concurrent handshake limiting // queue, set MAX_CONCURRENT_STREAMS on this server. server_ = builder.BuildAndStart(); - gpr_log(GPR_INFO, "Fake handshaker server listening on %s", address_.get()); + gpr_log(GPR_INFO, "Fake handshaker server listening on %s", + address_.c_str()); } ~FakeHandshakeServer() { server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0)); } - const char* address() { return address_.get(); } + const char* address() { return address_.c_str(); } private: - grpc_core::UniquePtr address_; + std::string address_; std::unique_ptr service_; std::unique_ptr server_; }; @@ -147,13 +149,13 @@ class TestServer { server_cq_ = grpc_completion_queue_create_for_next(nullptr); grpc_server_register_completion_queue(server_, server_cq_, nullptr); int port = grpc_pick_unused_port_or_die(); - GPR_ASSERT(grpc_core::JoinHostPort(&server_addr_, "localhost", port)); - GPR_ASSERT(grpc_server_add_secure_http2_port(server_, server_addr_.get(), + server_addr_ = grpc_core::JoinHostPort("localhost", port); + GPR_ASSERT(grpc_server_add_secure_http2_port(server_, server_addr_.c_str(), server_creds)); grpc_server_credentials_release(server_creds); grpc_server_start(server_); gpr_log(GPR_DEBUG, "Start TestServer %p. listen on %s", this, - server_addr_.get()); + server_addr_.c_str()); server_thd_ = std::unique_ptr(new std::thread(PollUntilShutdown, this)); } @@ -168,7 +170,7 @@ class TestServer { grpc_completion_queue_destroy(server_cq_); } - const char* address() { return server_addr_.get(); } + const char* address() { return server_addr_.c_str(); } static void PollUntilShutdown(const TestServer* self) { grpc_event ev = grpc_completion_queue_next( @@ -182,7 +184,7 @@ class TestServer { grpc_server* server_; grpc_completion_queue* server_cq_; std::unique_ptr server_thd_; - grpc_core::UniquePtr server_addr_; + std::string server_addr_; // Give this test server its own ALTS handshake server // so that we avoid competing for ALTS handshake server resources (e.g. // available HTTP2 streams on a globally shared handshaker subchannel) diff --git a/test/cpp/naming/address_sorting_test.cc b/test/cpp/naming/address_sorting_test.cc index 6f94635927d..38203bcfcca 100644 --- a/test/cpp/naming/address_sorting_test.cc +++ b/test/cpp/naming/address_sorting_test.cc @@ -108,19 +108,17 @@ class MockSourceAddrFactory : public address_sorting_source_addr_factory { !ipv6_supported_)) { return false; } - char* ip_addr_str; grpc_resolved_address dest_addr_as_resolved_addr; memcpy(&dest_addr_as_resolved_addr.addr, dest_addr, dest_addr->len); dest_addr_as_resolved_addr.len = dest_addr->len; - grpc_sockaddr_to_string(&ip_addr_str, &dest_addr_as_resolved_addr, - false /* normalize */); + std::string ip_addr_str = grpc_sockaddr_to_string( + &dest_addr_as_resolved_addr, false /* normalize */); auto it = dest_addr_to_src_addr_.find(ip_addr_str); if (it == dest_addr_to_src_addr_.end()) { - gpr_log(GPR_DEBUG, "can't find |%s| in dest to src map", ip_addr_str); - gpr_free(ip_addr_str); + gpr_log(GPR_DEBUG, "can't find |%s| in dest to src map", + ip_addr_str.c_str()); return false; } - gpr_free(ip_addr_str); grpc_resolved_address source_addr_as_resolved_addr = TestAddressToGrpcResolvedAddress(it->second); memcpy(source_addr->addr, &source_addr_as_resolved_addr.addr, @@ -180,13 +178,10 @@ void VerifyLbAddrOutputs(const grpc_core::ServerAddressList& addresses, std::vector expected_addrs) { EXPECT_EQ(addresses.size(), expected_addrs.size()); for (size_t i = 0; i < addresses.size(); ++i) { - char* ip_addr_str; - grpc_sockaddr_to_string(&ip_addr_str, &addresses[i].address(), - false /* normalize */); + std::string ip_addr_str = + grpc_sockaddr_to_string(&addresses[i].address(), false /* normalize */); EXPECT_EQ(expected_addrs[i], ip_addr_str); - gpr_free(ip_addr_str); } - grpc_core::ExecCtx exec_ctx; } /* We need to run each test case inside of its own diff --git a/test/cpp/naming/resolver_component_test.cc b/test/cpp/naming/resolver_component_test.cc index c5dfc777397..350806d6aa7 100644 --- a/test/cpp/naming/resolver_component_test.cc +++ b/test/cpp/naming/resolver_component_test.cc @@ -503,11 +503,10 @@ class CheckingResultHandler : public ResultHandler { std::vector* out) { for (size_t i = 0; i < addresses.size(); i++) { const grpc_core::ServerAddress& addr = addresses[i]; - char* str; - grpc_sockaddr_to_string(&str, &addr.address(), 1 /* normalize */); - gpr_log(GPR_INFO, "%s", str); - out->emplace_back(GrpcLBAddress(std::string(str), is_balancer)); - gpr_free(str); + std::string str = + grpc_sockaddr_to_string(&addr.address(), true /* normalize */); + gpr_log(GPR_INFO, "%s", str.c_str()); + out->emplace_back(GrpcLBAddress(std::move(str), is_balancer)); } } }; diff --git a/test/cpp/qps/driver.cc b/test/cpp/qps/driver.cc index 1f3176f5548..f26c11c8dca 100644 --- a/test/cpp/qps/driver.cc +++ b/test/cpp/qps/driver.cc @@ -347,11 +347,10 @@ std::unique_ptr RunScenario( cli_target += std::to_string(i); client_config.add_server_targets(cli_target); } else { - std::string host; - grpc_core::UniquePtr cli_target; - host = get_host(workers[i]); - grpc_core::JoinHostPort(&cli_target, host.c_str(), init_status.port()); - client_config.add_server_targets(cli_target.get()); + std::string host = get_host(workers[i]); + std::string cli_target = + grpc_core::JoinHostPort(host.c_str(), init_status.port()); + client_config.add_server_targets(cli_target.c_str()); } } diff --git a/test/cpp/qps/qps_worker.cc b/test/cpp/qps/qps_worker.cc index 78dae6207e0..8a725c03405 100644 --- a/test/cpp/qps/qps_worker.cc +++ b/test/cpp/qps/qps_worker.cc @@ -279,10 +279,9 @@ QpsWorker::QpsWorker(int driver_port, int server_port, std::unique_ptr builder = CreateQpsServerBuilder(); if (driver_port >= 0) { - grpc_core::UniquePtr server_address; - grpc_core::JoinHostPort(&server_address, "::", driver_port); + std::string server_address = grpc_core::JoinHostPort("::", driver_port); builder->AddListeningPort( - server_address.get(), + server_address.c_str(), GetCredentialsProvider()->GetServerCredentials(credential_type)); } builder->RegisterService(impl_.get()); diff --git a/test/cpp/qps/server_async.cc b/test/cpp/qps/server_async.cc index b6cb020a891..6a07accb151 100644 --- a/test/cpp/qps/server_async.cc +++ b/test/cpp/qps/server_async.cc @@ -80,9 +80,8 @@ class AsyncQpsServerTest final : public grpc::testing::Server { auto port_num = port(); // Negative port number means inproc server, so no listen port needed if (port_num >= 0) { - grpc_core::UniquePtr server_address; - grpc_core::JoinHostPort(&server_address, "::", port_num); - builder->AddListeningPort(server_address.get(), + std::string server_address = grpc_core::JoinHostPort("::", port_num); + builder->AddListeningPort(server_address.c_str(), Server::CreateServerCredentials(config)); } diff --git a/test/cpp/qps/server_callback.cc b/test/cpp/qps/server_callback.cc index 4b6f651e356..e4aa10946d2 100644 --- a/test/cpp/qps/server_callback.cc +++ b/test/cpp/qps/server_callback.cc @@ -102,9 +102,8 @@ class CallbackServer final : public grpc::testing::Server { auto port_num = port(); // Negative port number means inproc server, so no listen port needed if (port_num >= 0) { - grpc_core::UniquePtr server_address; - grpc_core::JoinHostPort(&server_address, "::", port_num); - builder->AddListeningPort(server_address.get(), + std::string server_address = grpc_core::JoinHostPort("::", port_num); + builder->AddListeningPort(server_address.c_str(), Server::CreateServerCredentials(config)); } diff --git a/test/cpp/qps/server_sync.cc b/test/cpp/qps/server_sync.cc index f69a8f07ace..19d406a167c 100644 --- a/test/cpp/qps/server_sync.cc +++ b/test/cpp/qps/server_sync.cc @@ -160,9 +160,8 @@ class SynchronousServer final : public grpc::testing::Server { auto port_num = port(); // Negative port number means inproc server, so no listen port needed if (port_num >= 0) { - grpc_core::UniquePtr server_address; - grpc_core::JoinHostPort(&server_address, "::", port_num); - builder->AddListeningPort(server_address.get(), + std::string server_address = grpc_core::JoinHostPort("::", port_num); + builder->AddListeningPort(server_address.c_str(), Server::CreateServerCredentials(config)); }