[master] 871d00207 Wrap checks for UDS path

Nils Goroll nils.goroll at uplex.de
Mon Nov 21 16:06:07 UTC 2022


commit 871d0020732e17642b445da8691f7dfe4efec5ea
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Nov 21 16:50:35 2022 +0100

    Wrap checks for UDS path
    
    This commit was guided by the cocci patch below, but lines to change
    were manually selected.
    
    Motivated by #3864
    
            --- 8< ---
    
    @@
    expression path;
    @@
    
    - *path == '/'
    + VUS_is(path)
    
    @@
    expression path;
    @@
    
    - *path != '/'
    + ! VUS_is(path)

diff --git a/bin/varnishd/mgt/mgt_acceptor.c b/bin/varnishd/mgt/mgt_acceptor.c
index 9cbeb81aa..c02941048 100644
--- a/bin/varnishd/mgt/mgt_acceptor.c
+++ b/bin/varnishd/mgt/mgt_acceptor.c
@@ -158,7 +158,7 @@ mk_listen_sock(const struct listen_arg *la, const struct suckaddr *sa)
 	ls->name = la->name;
 	ls->transport = la->transport;
 	ls->perms = la->perms;
-	if (*la->endpoint == '/')
+	if (VUS_is(la->endpoint))
 		ls->uds = 1;
 	VJ_master(JAIL_MASTER_PRIVPORT);
 	fail = mac_opensocket(ls);
@@ -273,7 +273,7 @@ MAC_Arg(const char *spec)
 		ARGV_ERR("Unix domain socket addresses must be"
 		    " absolute paths in -a (%s)\n", la->endpoint);
 
-	if (*la->endpoint == '/' && heritage.min_vcl_version < 41)
+	if (VUS_is(la->endpoint) && heritage.min_vcl_version < 41)
 		heritage.min_vcl_version = 41;
 
 	for (int i = 2; av[i] != NULL; i++) {
@@ -368,7 +368,7 @@ MAC_Arg(const char *spec)
 	else
 		AZ(la->perms);
 
-	if (*la->endpoint != '/')
+	if (! VUS_is(la->endpoint))
 		error = VSS_resolver(av[1], "80", mac_tcp, la, &err);
 	else
 		error = VUS_resolver(av[1], mac_uds, la, &err);
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 544359db9..50247a963 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -62,6 +62,7 @@
 #include "vtim.h"
 #include "waiter/mgt_waiter.h"
 #include "vsa.h"
+#include "vus.h"
 
 struct heritage		heritage;
 unsigned		d_flag = 0;
@@ -483,7 +484,7 @@ mgt_b_conv(const char *b_arg)
 	VSB_cat(vsb, "backend default ");
 	if (!strcasecmp(b_arg, "none"))
 		VSB_cat(vsb, "none;\n");
-	else if (*b_arg != '/')
+	else if (! VUS_is(b_arg))
 		VSB_printf(vsb, "{\n    .host = \"%s\";\n}\n", b_arg);
 	else
 		VSB_printf(vsb, "{\n    .path = \"%s\";\n}\n", b_arg);
diff --git a/bin/varnishtest/vtc_client.c b/bin/varnishtest/vtc_client.c
index 178aa1335..883aad017 100644
--- a/bin/varnishtest/vtc_client.c
+++ b/bin/varnishtest/vtc_client.c
@@ -186,7 +186,7 @@ client_connect(struct vtclog *vl, struct client *c)
 	int fd;
 
 	vtc_log(vl, 3, "Connect to %s", c->addr);
-	if (*c->addr == '/')
+	if (VUS_is(c->addr))
 		fd = client_uds_connect(vl, c->addr, 10., &err);
 	else
 		fd = client_tcp_connect(vl, c->addr, 10., &err);
diff --git a/bin/varnishtest/vtc_server.c b/bin/varnishtest/vtc_server.c
index 783be0a2e..437f685c6 100644
--- a/bin/varnishtest/vtc_server.c
+++ b/bin/varnishtest/vtc_server.c
@@ -216,7 +216,7 @@ server_listen(struct server *s)
 
 	if (s->sock >= 0)
 		VTCP_close(&s->sock);
-	if (*s->listen != '/')
+	if (! VUS_is(s->listen))
 		server_listen_tcp(s, &err);
 	else
 		server_listen_uds(s, &err);
@@ -248,7 +248,7 @@ server_conn(void *priv, struct vtclog *vl)
 	fd = accept(s->sock, addr, &l);
 	if (fd < 0)
 		vtc_fatal(vl, "Accept failed: %s", strerror(errno));
-	if (*s->listen != '/') {
+	if (! VUS_is(s->listen)) {
 		VTCP_hisname(fd, abuf, sizeof abuf, pbuf, sizeof pbuf);
 		vtc_log(vl, 3, "accepted fd %d %s %s", fd, abuf, pbuf);
 	} else
@@ -431,7 +431,7 @@ cmd_server_gen_vcl(struct vsb *vsb)
 		if (s->sock < 0 && s->fd >= 0) /* dispatch instance */
 			continue;
 
-		if (*s->listen != '/')
+		if (! VUS_is(s->listen))
 			VSB_printf(vsb,
 				   "backend %s { .host = \"%s\"; "
 				   ".port = \"%s\"; }\n",
@@ -456,7 +456,7 @@ cmd_server_gen_haproxy_conf(struct vsb *vsb)
 
 	AZ(pthread_mutex_lock(&server_mtx));
 	VTAILQ_FOREACH(s, &servers, list) {
-		if (*s->listen != '/')
+		if (! VUS_is(s->listen))
 			VSB_printf(vsb,
 			   "\n    backend be%s\n"
 			   "\tserver srv%s %s:%s\n",
@@ -465,7 +465,7 @@ cmd_server_gen_haproxy_conf(struct vsb *vsb)
 			INCOMPL();
 	}
 	VTAILQ_FOREACH(s, &servers, list) {
-		if (*s->listen != '/')
+		if (! VUS_is(s->listen))
 			VSB_printf(vsb,
 			   "\n    frontend http%s\n"
 			   "\tuse_backend be%s\n"
diff --git a/include/vus.h b/include/vus.h
index 157b65102..c2daf8f85 100644
--- a/include/vus.h
+++ b/include/vus.h
@@ -36,3 +36,9 @@ int VUS_resolver(const char *path, vus_resolved_f *func, void *priv,
 		 const char **err);
 int VUS_bind(const struct sockaddr_un *uds, const char **errp);
 int VUS_connect(const char *path, int msec);
+
+static inline int
+VUS_is(const char *path)
+{
+	return (*path == '/');
+}
diff --git a/lib/libvarnish/vus.c b/lib/libvarnish/vus.c
index 0f39fb601..07e3bf193 100644
--- a/lib/libvarnish/vus.c
+++ b/lib/libvarnish/vus.c
@@ -46,7 +46,7 @@ sun_init(struct sockaddr_un *uds, const char *path, const char **err)
 {
 	AN(uds);
 	AN(path);
-	assert(*path == '/');
+	assert(VUS_is(path));
 
 	if (err)
 		*err = NULL;
diff --git a/vmod/vmod_debug_dyn.c b/vmod/vmod_debug_dyn.c
index 6b562cfc7..8e9676c2c 100644
--- a/vmod/vmod_debug_dyn.c
+++ b/vmod/vmod_debug_dyn.c
@@ -41,6 +41,7 @@
 
 #include "vsa.h"
 #include "vss.h"
+#include "vus.h"
 #include "vcc_debug_if.h"
 
 struct xyzzy_debug_dyn {
@@ -178,7 +179,7 @@ dyn_uds_init(VRT_CTX, struct xyzzy_debug_dyn_uds *uds, VCL_STRING path)
 		VRT_fail(ctx, "path is NULL");
 		return (-1);
 	}
-	if (*path != '/') {
+	if (! VUS_is(path)) {
 		VRT_fail(ctx, "path must be an absolute path: %s", path);
 		return (-1);
 	}


More information about the varnish-commit mailing list