]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
Use oc_text_buf for internal_get_url()
authorDaniel Lenski <dlenski@gmail.com>
Mon, 5 Apr 2021 15:37:00 +0000 (08:37 -0700)
committerDavid Woodhouse <dwmw2@infradead.org>
Tue, 6 Apr 2021 14:33:53 +0000 (15:33 +0100)
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 <dlenski@gmail.com>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
http.c

diff --git a/http.c b/http.c
index 261cf91664157163c82a31870fa603a1c54698f1..bb82a22b801f77cbc255e055ed2bfa446a02803a 100644 (file)
--- 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)