From: Stephan Bosch Date: Sat, 20 Nov 2021 18:08:01 +0000 (+0100) Subject: lib: uri-util - Make construction of URI host name robust to IP literals. X-Git-Tag: 2.4.0~4695 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=673c837159b76e8568b76df61248dceef09ae88d;p=thirdparty%2Fdovecot%2Fcore.git lib: uri-util - Make construction of URI host name robust to IP literals. When only a host name (string) is set, it may still be an IP address. This change makes sure it is correctly written to the URL when it is IPv6; don't trust the caller to get it right. --- diff --git a/src/lib/uri-util.c b/src/lib/uri-util.c index 8cd6d48170..5713a1438d 100644 --- a/src/lib/uri-util.c +++ b/src/lib/uri-util.c @@ -1291,15 +1291,16 @@ void uri_append_host_ip(string_t *out, const struct ip_addr *host_ip) void uri_append_host(string_t *out, const struct uri_host *host) { + struct ip_addr ip; + if (host->ip.family != 0) uri_append_host_ip(out, &host->ip); else { i_assert(host->name != NULL); - /* Assume IPv6 literal if starts with '['; avoid encoding */ - if (*host->name == '[') - str_append(out, host->name); - else + if (net_addr2ip(host->name, &ip) < 0) uri_append_host_name(out, host->name); + else + uri_append_host_ip(out, &ip); } }