const char *xpath_fmt = "string(/domain/devices/graphics[@type='%s']/%s)";
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
char *xpath = NULL;
- char *listen_addr = NULL;
+ g_autofree char *listen_addr = NULL;
int port = 0;
int tls_port = 0;
- char *type_conn = NULL;
- char *sockpath = NULL;
- char *passwd = NULL;
+ g_autofree char *type_conn = NULL;
+ g_autofree char *sockpath = NULL;
+ g_autofree char *passwd = NULL;
int tmp;
bool params = false;
- virSocketAddr addr;
/* Create our XPATH lookup for the current display's port */
VIR_FREE(xpath);
/* Attempt to get the listening addr if set for the current
* graphics scheme */
- VIR_FREE(listen_addr);
listen_addr = virXPathString(xpath, ctxt);
VIR_FREE(xpath);
xpath = g_strdup_printf(xpath_fmt, scheme, "listen/@type");
/* Attempt to get the type of spice connection */
- VIR_FREE(type_conn);
type_conn = virXPathString(xpath, ctxt);
VIR_FREE(xpath);
if (STREQ_NULLABLE(type_conn, "socket")) {
- if (!sockpath) {
- xpath = g_strdup_printf(xpath_fmt, scheme, "listen/@socket");
+ xpath = g_strdup_printf(xpath_fmt, scheme, "listen/@socket");
- sockpath = virXPathString(xpath, ctxt);
+ sockpath = virXPathString(xpath, ctxt);
- VIR_FREE(xpath);
- }
+ VIR_FREE(xpath);
}
if (!port && !tls_port && !sockpath)
listen_addr = virXPathString(xpath, ctxt);
VIR_FREE(xpath);
} else {
+ virSocketAddr addr;
+
/* If listen_addr is 0.0.0.0 or [::] we should try to parse URI and set
- * listen_addr based on current URI. */
+ * listen_addr based on current URI. If that fails we'll print
+ * 'localhost' as the address as INADDR_ANY won't help the user. */
if (virSocketAddrParse(&addr, listen_addr, AF_UNSPEC) > 0 &&
virSocketAddrIsWildcard(&addr)) {
virConnectPtr conn = ((virshControl *)(ctl->privData))->conn;
- char *uriStr = virConnectGetURI(conn);
- virURI *uri = NULL;
+ g_autofree char *uriStr = virConnectGetURI(conn);
+ g_autoptr(virURI) uri = NULL;
- if (uriStr) {
- uri = virURIParse(uriStr);
- VIR_FREE(uriStr);
- }
-
- /* It's safe to free the listen_addr even if parsing of URI
- * fails, if there is no listen_addr we will print "localhost". */
- VIR_FREE(listen_addr);
+ g_clear_pointer(&listen_addr, g_free);
- if (uri) {
+ if (uriStr && (uri = virURIParse(uriStr)))
listen_addr = g_strdup(uri->server);
- virURIFree(uri);
- }
}
}
xpath = g_strdup_printf(xpath_fmt, scheme, "@passwd");
/* Attempt to get the password */
- VIR_FREE(passwd);
passwd = virXPathString(xpath, ctxt);
VIR_FREE(xpath);
else
virBufferAsprintf(&buf, "%s", listen_addr);
- /* Free socket to prepare the pointer for the next iteration */
- VIR_FREE(sockpath);
-
/* Add the port */
if (port) {
if (STREQ(scheme, "vnc")) {
cleanup:
VIR_FREE(xpath);
- VIR_FREE(type_conn);
- VIR_FREE(sockpath);
- VIR_FREE(passwd);
- VIR_FREE(listen_addr);
return virBufferContentAndReset(&buf);
}