[master] 0226b32 Don't use sprintf(3)

Poul-Henning Kamp phk at FreeBSD.org
Sat Jan 14 20:49:05 CET 2017


commit 0226b328c2dcbae93b263fa4da033a9417f7e814
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sat Jan 14 19:47:58 2017 +0000

    Don't use sprintf(3)

diff --git a/bin/varnishadm/varnishadm.c b/bin/varnishadm/varnishadm.c
index b3823d8..421e3c5 100644
--- a/bin/varnishadm/varnishadm.c
+++ b/bin/varnishadm/varnishadm.c
@@ -62,6 +62,7 @@
 #include "vapi/vsm.h"
 #include "vas.h"
 #include "vcli.h"
+#include "vdef.h"
 #include "vnum.h"
 #include "vtcp.h"
 
@@ -302,7 +303,7 @@ interactive(int sock)
 				RL_EXIT(1);
 			}
 
-			sprintf(buf, "%u\n", status);
+			bprintf(buf, "%u\n", status);
 			u = write(1, buf, strlen(buf));
 			if (answer) {
 				u = write(1, answer, strlen(answer));
@@ -352,7 +353,7 @@ pass(int sock)
 				RL_EXIT(1);
 			}
 
-			sprintf(buf, "%u\n", status);
+			bprintf(buf, "%u\n", status);
 			u = write(1, buf, strlen(buf));
 			if (answer) {
 				u = write(1, answer, strlen(answer));
diff --git a/bin/varnishd/cache/cache_shmlog.c b/bin/varnishd/cache/cache_shmlog.c
index a294d65..cc3345d 100644
--- a/bin/varnishd/cache/cache_shmlog.c
+++ b/bin/varnishd/cache/cache_shmlog.c
@@ -401,7 +401,7 @@ VSLb_bin(struct vsl_log *vsl, enum VSL_tag_e tag, ssize_t len, const void *ptr)
 	assert(VSL_END(vsl->wlp, len * 2 + 1) < vsl->wle);
 	p = VSL_DATA(vsl->wlp);
 	for (ll = 0; ll < len; ll++) {
-		(void)sprintf(p, "%02x", *pp++);
+		assert(snprintf(p, 3, "%02x", *pp++) == 2);
 		p += 2;
 	}
 	if (suff)
diff --git a/bin/varnishd/http2/cache_http2_deliver.c b/bin/varnishd/http2/cache_http2_deliver.c
index db94d1d..b69b45d 100644
--- a/bin/varnishd/http2/cache_http2_deliver.c
+++ b/bin/varnishd/http2/cache_http2_deliver.c
@@ -130,7 +130,7 @@ h2_deliver(struct req *req, struct boc *boc, int sendbody)
 		*p++ = 0x18;
 		*p++ = 0x03;
 
-		(void)sprintf((char*)p, "%03d", req->resp->status);
+		assert(snprintf((char*)p, 4, "%03d", req->resp->status) == 3);
 		p += 3;
 		break;
 	}
diff --git a/bin/varnishd/mgt/mgt_child.c b/bin/varnishd/mgt/mgt_child.c
index d46f42d..f6fc0c3 100644
--- a/bin/varnishd/mgt/mgt_child.c
+++ b/bin/varnishd/mgt/mgt_child.c
@@ -265,7 +265,7 @@ child_sigsegv_handler(int s, siginfo_t *si, void *c)
 	(void)s;
 	(void)c;
 
-	sprintf(buf, "Segmentation fault by instruction at %p", si->si_addr);
+	bprintf(buf, "Segmentation fault by instruction at %p", si->si_addr);
 	VAS_Fail(__func__,
 		 __FILE__,
 		 __LINE__,
diff --git a/bin/varnishtest/vtc_http.c b/bin/varnishtest/vtc_http.c
index 70e226c..419491a 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -501,7 +501,7 @@ http_splitheader(struct http *hp, int req)
 	assert(*p == '\0');
 
 	for (n = 0; n < 3 || hh[n] != NULL; n++) {
-		sprintf(buf, "http[%2d] ", n);
+		bprintf(buf, "http[%2d] ", n);
 		vtc_dump(hp->vl, 4, buf, hh[n], -1);
 	}
 }
@@ -615,7 +615,7 @@ http_swallow_body(struct http *hp, char * const *hh, int body)
 		vtc_dump(hp->vl, 4, "body", hp->body, ll);
 		ll = hp->rxbuf + hp->prxbuf - hp->body;
 		hp->bodyl = ll;
-		sprintf(hp->bodylen, "%d", ll);
+		bprintf(hp->bodylen, "%d", ll);
 		return;
 	}
 	p = http_find_header(hh, "content-length");
@@ -635,7 +635,7 @@ http_swallow_body(struct http *hp, char * const *hh, int body)
 		vtc_dump(hp->vl, 4, "rxeof", hp->body, ll);
 	}
 	hp->bodyl = ll;
-	sprintf(hp->bodylen, "%d", ll);
+	bprintf(hp->bodylen, "%d", ll);
 }
 
 /**********************************************************************
@@ -1197,7 +1197,7 @@ cmd_http_rxchunk(CMD_ARGS)
 	if (i == 0) {
 		ll = hp->rxbuf + hp->prxbuf - hp->body;
 		hp->bodyl = ll;
-		sprintf(hp->bodylen, "%d", ll);
+		bprintf(hp->bodylen, "%d", ll);
 		vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
 	}
 }
@@ -1952,7 +1952,7 @@ xxx(void)
 	for(n = 0;  n < 999999999; n++) {
 		*ibuf = 0;
 		for (j = 0; j < 7; j++) {
-			sprintf(strchr(ibuf, 0), "%x",
+			snprintf(strchr(ibuf, 0), 5, "%x",
 			    (unsigned)random() & 0xffff);
 			vz.next_in = TRUST_ME(ibuf);
 			vz.avail_in = strlen(ibuf);
diff --git a/lib/libvarnish/vcli_proto.c b/lib/libvarnish/vcli_proto.c
index e9ea299..ee36362 100644
--- a/lib/libvarnish/vcli_proto.c
+++ b/lib/libvarnish/vcli_proto.c
@@ -65,7 +65,7 @@ VCLI_AuthResponse(int S_fd, const char *challenge,
 	SHA256_Update(&ctx, "\n", 1);
 	SHA256_Final(buf, &ctx);
 	for(i = 0; i < SHA256_LEN; i++)
-		sprintf(response + 2 * i, "%02x", buf[i]);
+		assert(snprintf(response + 2 * i, 3, "%02x", buf[i]) == 2);
 }
 
 int
diff --git a/lib/libvcc/vcc_acl.c b/lib/libvcc/vcc_acl.c
index 8a8a87a..532d1bf 100644
--- a/lib/libvcc/vcc_acl.c
+++ b/lib/libvcc/vcc_acl.c
@@ -458,7 +458,7 @@ vcc_acl_emit(struct vcc *tl, const char *acln, int anon)
 }
 
 void
-vcc_Acl_Hack(struct vcc *tl, char *b)
+vcc_Acl_Hack(struct vcc *tl, char *b, size_t bl)
 {
 	char acln[32];
 	unsigned tcond;
@@ -469,8 +469,8 @@ vcc_Acl_Hack(struct vcc *tl, char *b)
 	bprintf(acln, "%u", tl->unique++);
 	vcc_acl_entry(tl);
 	vcc_acl_emit(tl, acln, 1);
-	sprintf(b, "%smatch_acl_anon_%s(ctx, \v1)",
-	    (tcond == T_NEQ ? "!" : ""), acln);
+	assert(snprintf(b, bl - 1, "%smatch_acl_anon_%s(ctx, \v1)",
+	    (tcond == T_NEQ ? "!" : ""), acln) < bl - 1);
 }
 
 void
diff --git a/lib/libvcc/vcc_backend.c b/lib/libvcc/vcc_backend.c
index a5b5b5e..7909044 100644
--- a/lib/libvcc/vcc_backend.c
+++ b/lib/libvcc/vcc_backend.c
@@ -478,7 +478,7 @@ vcc_ParseBackend(struct vcc *tl)
 	t_be = tl->t;
 	vcc_NextToken(tl);
 
-	sprintf(vgcname, "vgc_backend_%.*s", PF(t_be));
+	bprintf(vgcname, "vgc_backend_%.*s", PF(t_be));
 	Fh(tl, 0, "\nstatic struct director *%s;\n", vgcname);
 
 	sym = VCC_HandleSymbol(tl, t_be, BACKEND, "%s", vgcname);
diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index d4b3964..fd7daf1 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -233,7 +233,7 @@ struct method {
 /* vcc_acl.c */
 
 void vcc_ParseAcl(struct vcc *tl);
-void vcc_Acl_Hack(struct vcc *tl, char *b);
+void vcc_Acl_Hack(struct vcc *tl, char *b, size_t bl);
 
 /* vcc_action.c */
 int vcc_ParseAction(struct vcc *tl);
diff --git a/lib/libvcc/vcc_expr.c b/lib/libvcc/vcc_expr.c
index ce7ab89..d97c5f7 100644
--- a/lib/libvcc/vcc_expr.c
+++ b/lib/libvcc/vcc_expr.c
@@ -1197,7 +1197,7 @@ vcc_expr_cmp(struct vcc *tl, struct expr **e, vcc_type_t fmt)
 		return;
 	}
 	if ((*e)->fmt == IP && (tl->t->tok == T_EQ || tl->t->tok == T_NEQ)) {
-		vcc_Acl_Hack(tl, buf);
+		vcc_Acl_Hack(tl, buf, sizeof buf);
 		*e = vcc_expr_edit(BOOL, buf, *e, NULL);
 		return;
 	}
diff --git a/lib/libvcc/vcc_utils.c b/lib/libvcc/vcc_utils.c
index a95c20c..89ccccc 100644
--- a/lib/libvcc/vcc_utils.c
+++ b/lib/libvcc/vcc_utils.c
@@ -66,7 +66,7 @@ vcc_regexp(struct vcc *tl)
 		return (NULL);
 	}
 	VRE_free(&t);
-	sprintf(buf, "VGC_re_%u", tl->unique++);
+	bprintf(buf, "VGC_re_%u", tl->unique++);
 	p = TlAlloc(tl, strlen(buf) + 1);
 	strcpy(p, buf);
 
@@ -124,7 +124,7 @@ vcc_suckaddr(struct vcc *tl, const char *host, const struct suckaddr *vsa,
 
 	q = TlAlloc(tl, 40);
 	AN(q);
-	sprintf(q, "(const void*)suckaddr_%u", tl->unique);
+	assert(snprintf(q, 40, "(const void*)suckaddr_%u", tl->unique) < 40);
 	*ip = q;
 	tl->unique++;
 }
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index 9b4e5e7..023cdc3 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -168,7 +168,7 @@ vmod_blob2hex(VRT_CTX, VCL_BLOB b)
 	p = s;
 	q = b->priv;
 	for (i = 0; i < b->len; i++) {
-		sprintf(p, "%02x", *q);
+		assert(snprintf(p, 3, "%02x", *q) == 2);
 		p += 2;
 		q += 1;
 	}
diff --git a/lib/libvmod_directors/shard_cfg.c b/lib/libvmod_directors/shard_cfg.c
index 3f91aab..20e9b32 100644
--- a/lib/libvmod_directors/shard_cfg.c
+++ b/lib/libvmod_directors/shard_cfg.c
@@ -264,7 +264,7 @@ shardcfg_hashcircle(struct sharddir *shardd, VCL_INT replicas, enum alg_e alg)
 		char s[len];
 
 		for (j = 0; j < replicas; j++) {
-			sprintf(s, "%s%d", ident, j);
+			assert(snprintf(s, len, "%s%d", ident, j) < len);
 			shardd->hashcircle[i * replicas + j].point =
 			    shard_hash_f[alg](s);
 			shardd->hashcircle[i * replicas + j].host = i;



More information about the varnish-commit mailing list