[master] ecddbee Move the necessary strdup() into parse_string and comment that we deliberately leak that copy, so the cmd functions we call don't have to worry about strdup'ing individual arguments.

Poul-Henning Kamp phk at FreeBSD.org
Thu Apr 30 15:32:28 CEST 2015


commit ecddbee089d1a00133971b8383d96406263376c9
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Thu Apr 30 13:14:53 2015 +0000

    Move the necessary strdup() into parse_string and comment that we
    deliberately leak that copy, so the cmd functions we call don't
    have to worry about strdup'ing individual arguments.

diff --git a/bin/varnishtest/vtc.c b/bin/varnishtest/vtc.c
index 168423c..653045f 100644
--- a/bin/varnishtest/vtc.c
+++ b/bin/varnishtest/vtc.c
@@ -255,20 +255,28 @@ extmacro_def(const char *name, const char *fmt, ...)
 }
 
 /**********************************************************************
- * Execute a file
+ * Parse a string
+ *
+ * We make a copy of the string and deliberately leak it, so that all
+ * the cmd functions we call don't have to strdup(3) all over the place.
+ *
+ * Static checkers like Coverity may bitch about this, but we don't care.
  */
 
 void
-parse_string(char *buf, const struct cmds *cmd, void *priv, struct vtclog *vl)
+parse_string(const char *spec, const struct cmds *cmd, void *priv,
+    struct vtclog *vl)
 {
 	char *token_s[MAX_TOKENS], *token_e[MAX_TOKENS];
 	struct vsb *token_exp[MAX_TOKENS];
-	char *p, *q, *f;
+	char *p, *q, *f, *buf;
 	int nest_brace;
 	int tn;
 	const struct cmds *cp;
 
-	assert(buf != NULL);
+	AN(spec);
+	buf = strdup(spec);
+	AN(buf);
 	for (p = buf; *p != '\0'; p++) {
 		if (vtc_error || vtc_stop)
 			break;
@@ -359,8 +367,9 @@ parse_string(char *buf, const struct cmds *cmd, void *priv, struct vtclog *vl)
 			if (NULL == strstr(token_s[tn], "${"))
 				continue;
 			token_exp[tn] = macro_expand(vl, token_s[tn]);
-			if (vtc_error)
+			if (vtc_error) {
 				return;
+			}
 			token_s[tn] = VSB_data(token_exp[tn]);
 			token_e[tn] = strchr(token_s[tn], '\0');
 		}
@@ -619,7 +628,6 @@ exec_file(const char *fn, const char *script, const char *tmpdir,
     char *logbuf, unsigned loglen)
 {
 	unsigned old_err;
-	char *p;
 	FILE *f;
 	struct extmacro *m;
 
@@ -658,11 +666,8 @@ exec_file(const char *fn, const char *script, const char *tmpdir,
 	vtc_stop = 0;
 	vtc_log(vltop, 1, "TEST %s starting", fn);
 
-	p = strdup(script);
-	AN(p);
-
 	vtc_thread = pthread_self();
-	parse_string(p, cmds, NULL, vltop);
+	parse_string(script, cmds, NULL, vltop);
 	old_err = vtc_error;
 	vtc_stop = 1;
 	vtc_log(vltop, 1, "RESETTING after %s", fn);
diff --git a/bin/varnishtest/vtc.h b/bin/varnishtest/vtc.h
index 29f275a..7837052 100644
--- a/bin/varnishtest/vtc.h
+++ b/bin/varnishtest/vtc.h
@@ -54,7 +54,7 @@ struct cmds {
 	cmd_f		*cmd;
 };
 
-void parse_string(char *buf, const struct cmds *cmd, void *priv,
+void parse_string(const char *spec, const struct cmds *cmd, void *priv,
     struct vtclog *vl);
 
 cmd_f cmd_delay;
diff --git a/bin/varnishtest/vtc_http.c b/bin/varnishtest/vtc_http.c
index 6cb3d79..229663d 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -1256,7 +1256,6 @@ cmd_http_loop(CMD_ARGS)
 {
 	struct http *hp;
 	unsigned n, m;
-	char *s;
 
 	CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
 	AN(av[1]);
@@ -1265,9 +1264,7 @@ cmd_http_loop(CMD_ARGS)
 	n = strtoul(av[1], NULL, 0);
 	for (m = 1 ; m <= n; m++) {
 		vtc_log(vl, 4, "Loop #%u", m);
-		s = strdup(av[2]);
-		AN(s);
-		parse_string(s, cmd, hp, vl);
+		parse_string(av[2], cmd, hp, vl);
 	}
 }
 
@@ -1331,7 +1328,6 @@ int
 http_process(struct vtclog *vl, const char *spec, int sock, int *sfd)
 {
 	struct http *hp;
-	char *s;
 	int retval;
 
 	(void)sfd;
@@ -1349,10 +1345,7 @@ http_process(struct vtclog *vl, const char *spec, int sock, int *sfd)
 	AN(hp->rxbuf);
 	AN(hp->vsb);
 
-	s = strdup(spec);
-	AN(s);
-	parse_string(s, http_cmds, hp, vl);
-	free(s);
+	parse_string(spec, http_cmds, hp, vl);
 	retval = hp->fd;
 	VSB_delete(hp->vsb);
 	free(hp->rxbuf);
diff --git a/bin/varnishtest/vtc_logexp.c b/bin/varnishtest/vtc_logexp.c
index 3afec94..b05470d 100644
--- a/bin/varnishtest/vtc_logexp.c
+++ b/bin/varnishtest/vtc_logexp.c
@@ -453,16 +453,11 @@ static const struct cmds logexp_cmds[] = {
 static void
 logexp_spec(struct logexp *le, const char *spec)
 {
-	char *s;
-
 	CHECK_OBJ_NOTNULL(le, LOGEXP_MAGIC);
 
 	logexp_delete_tests(le);
 
-	s = strdup(spec);
-	AN(s);
-	parse_string(s, logexp_cmds, le, le->vl);
-	free(s);
+	parse_string(spec, logexp_cmds, le, le->vl);
 }
 
 void
diff --git a/bin/varnishtest/vtc_sema.c b/bin/varnishtest/vtc_sema.c
index 2ebfdee..1cab9d1 100644
--- a/bin/varnishtest/vtc_sema.c
+++ b/bin/varnishtest/vtc_sema.c
@@ -61,9 +61,10 @@ sema_new(char *name, struct vtclog *vl)
 
 	ALLOC_OBJ(r, SEMA_MAGIC);
 	AN(r);
-	r->name = name;
+	AN(name);
 	if (*name != 'r')
 		vtc_log(vl, 0, "Sema name must start with 'r' (%s)", name);
+	r->name = name;
 
 	AZ(pthread_mutex_init(&r->mtx, NULL));
 	AZ(pthread_cond_init(&r->cond, NULL));
diff --git a/bin/varnishtest/vtc_varnish.c b/bin/varnishtest/vtc_varnish.c
index 7f87479..a50166e 100644
--- a/bin/varnishtest/vtc_varnish.c
+++ b/bin/varnishtest/vtc_varnish.c
@@ -75,7 +75,6 @@ struct varnish {
 	struct VSM_data		*vd;		/* vsc use */
 
 	unsigned		vsl_tag_count[256];
-	unsigned		vsl_sleep;
 };
 
 #define NONSENSE	"%XJEIFLH|)Xspa8P"



More information about the varnish-commit mailing list