From fded24a2b4e3ddf246ec9dd6d4b98b53b375899b Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Mon, 5 Apr 2021 08:37:00 -0700 Subject: [PATCH] Use oc_text_buf for internal_get_url() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Commit c8ca180f ("factor out internal_get_url function") consolidated two pieces of code which reconstruct the URL from its constituent parts into a single internal_get_url() function. Both the original versions used oc_text_buf() and built up the URL string in a simple and readable fashion. The new internal_get_url() function used asprintf with "%s%s%s" and ternary operators ?"/":"" in an apparent attempt to win an obfuscated C contest. Unsurprisingly, the first attempt was buggy; this was subsequently fixed in commit f78f0eccc ("bugfix internal_get_url") but the horridness remained. In addition, the original two versions had slightly different behaviour — the one in auth-juniper.c would *always* append a trailing / at the end of the URL string even if vpninfo->urlpath was NULL, while the one in AnyConnect's auth.c would only do so if it was non-NULL (including when it's set to ""). As well as switching back to using oc_text_buf to build up the string so that we can read the code without our eyes bleeding, fix it to always add the trailing / even when vpninfo->urlpath is NULL. Signed-off-by: Daniel Lenski Signed-off-by: David Woodhouse --- http.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/http.c b/http.c index 261cf916..bb82a22b 100644 --- a/http.c +++ b/http.c @@ -854,12 +854,25 @@ int internal_parse_url(const char *url, char **res_proto, char **res_host, char *internal_get_url(struct openconnect_info *vpninfo) { + struct oc_text_buf *buf = buf_alloc(); char *url; - if (asprintf(&url, "https://%s%s%s", vpninfo->hostname, - vpninfo->urlpath ? "/" : "", vpninfo->urlpath ? : "") < 0) + + buf_append(buf, "https://%s", vpninfo->hostname); + if (vpninfo->port != 443) + buf_append(buf, ":%d", vpninfo->port); + buf_append(buf, "/"); + if (vpninfo->urlpath) + buf_append(buf, "%s", vpninfo->urlpath); + + if (buf_error(buf)) { + buf_free(buf); return NULL; - else + } else { + url = buf->data; + buf->data = NULL; + buf_free(buf); return url; + } } void openconnect_clear_cookies(struct openconnect_info *vpninfo) -- 2.50.1