From 0c53e8c2d0a9cb40eba8bab4b91d6921037896f5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 4 Dec 2013 14:27:28 +0100 Subject: system/Resolver: use std::string to allocate internal buffer No GLib memory allocation. --- src/system/Resolver.cxx | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'src/system') diff --git a/src/system/Resolver.cxx b/src/system/Resolver.cxx index 6e0870403..0cfb754ec 100644 --- a/src/system/Resolver.cxx +++ b/src/system/Resolver.cxx @@ -22,8 +22,6 @@ #include "util/Error.hxx" #include "util/Domain.hxx" -#include - #ifndef WIN32 #include #include @@ -107,19 +105,19 @@ resolve_host_port(const char *host_port, unsigned default_port, int flags, int socktype, Error &error) { - char *p = g_strdup(host_port); - const char *host = p, *port = NULL; + std::string p(host_port); + const char *host = p.c_str(), *port = nullptr; if (host_port[0] == '[') { /* IPv6 needs enclosing square braces, to differentiate between IP colons and the port separator */ - char *q = strchr(p + 1, ']'); - if (q != NULL && q[1] == ':' && q[2] != 0) { - *q = 0; + size_t q = p.find(']', 1); + if (q != p.npos && p[q + 1] == ':' && p[q + 2] != 0) { + p[q] = 0; + port = host + q + 2; ++host; - port = q + 2; } } @@ -127,10 +125,11 @@ resolve_host_port(const char *host_port, unsigned default_port, /* port is after the colon, but only if it's the only colon (don't split IPv6 addresses) */ - char *q = strchr(p, ':'); - if (q != NULL && q[1] != 0 && strchr(q + 1, ':') == NULL) { - *q = 0; - port = q + 1; + auto q = p.find(':'); + if (q != p.npos && p[q + 1] != 0 && + p.find(':', q + 1) == p.npos) { + p[q] = 0; + port = host + q + 1; } } @@ -151,7 +150,6 @@ resolve_host_port(const char *host_port, unsigned default_port, struct addrinfo *ai; int ret = getaddrinfo(host, port, &hints, &ai); - g_free(p); if (ret != 0) { error.Format(resolver_domain, ret, "Failed to look up '%s': %s", -- cgit v1.2.3