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>
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)