[master] 6e25bf69b Two more uses for WS_VSB()

Poul-Henning Kamp phk at FreeBSD.org
Mon Feb 10 11:24:06 UTC 2020


commit 6e25bf69b9d236d1479371beb678f486dcf5fa71
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Feb 10 11:23:36 2020 +0000

    Two more uses for WS_VSB()

diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index df95f3322..2297121e0 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -1228,28 +1228,32 @@ http_ForceHeader(struct http *to, const char *hdr, const char *val)
 void
 http_PrintfHeader(struct http *to, const char *fmt, ...)
 {
-	va_list ap;
-	unsigned l, n;
+	va_list ap, ap2;
+	struct vsb vsb[1];
+	size_t sz;
+	char *p;
 
 	CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
-	l = WS_ReserveAll(to->ws);
+
 	va_start(ap, fmt);
-	n = vsnprintf(to->ws->f, l, fmt, ap);
-	va_end(ap);
-	if (n + 1 >= l || to->nhd >= to->shd) {
+	va_copy(ap2, ap);
+
+	WS_VSB_new(vsb, to->ws);
+	VSB_vprintf(vsb, fmt, ap);
+	p = WS_VSB_finish(vsb, to->ws, &sz);
+
+	if (p == NULL || to->nhd >= to->shd) {
 		http_fail(to);
-		va_start(ap, fmt);
-		VSLbv(to->vsl, SLT_LostHeader, fmt, ap);
-		va_end(ap);
-		WS_Release(to->ws, 0);
-		return;
+		VSLbv(to->vsl, SLT_LostHeader, fmt, ap2);
+	} else {
+		to->hd[to->nhd].b = p;
+		to->hd[to->nhd].e = p + sz;
+		to->hdf[to->nhd] = 0;
+		http_VSLH(to, to->nhd);
+		to->nhd++;
 	}
-	to->hd[to->nhd].b = to->ws->f;
-	to->hd[to->nhd].e = to->ws->f + n;
-	to->hdf[to->nhd] = 0;
-	WS_Release(to->ws, n + 1);
-	http_VSLH(to, to->nhd);
-	to->nhd++;
+	va_end(ap);
+	va_end(ap2);
 }
 
 void
diff --git a/bin/varnishd/cache/cache_vrt_filter.c b/bin/varnishd/cache/cache_vrt_filter.c
index b6444d886..2bbdcf862 100644
--- a/bin/varnishd/cache/cache_vrt_filter.c
+++ b/bin/varnishd/cache/cache_vrt_filter.c
@@ -248,30 +248,17 @@ typedef void filter_list_t(void *, struct vsb *vsb);
 static const char *
 filter_on_ws(struct ws *ws, filter_list_t *func, void *arg)
 {
-	unsigned u;
 	struct vsb vsb[1];
+	const char *p;
 
 	AN(func);
 	AN(arg);
-	u = WS_ReserveAll(ws);
-	if (u == 0) {
-		WS_Release(ws, 0);
-		WS_MarkOverflow(ws);
-		return (NULL);
-	}
-	AN(VSB_new(vsb, ws->f, u, VSB_FIXEDLEN));
+	WS_VSB_new(vsb, ws);
 	func(arg, vsb);
-	if (VSB_finish(vsb)) {
-		WS_Release(ws, 0);
-		WS_MarkOverflow(ws);
-		return (NULL);
-	}
-	if (VSB_len(vsb)) {
-		WS_Release(ws, VSB_len(vsb) + 1);
-		return (VSB_data(vsb) + 1);
-	}
-	WS_Release(ws, 0);
-	return ("");
+	p = WS_VSB_finish(vsb, ws, NULL);
+	if (p == NULL)
+		p = "";
+	return (p);
 }
 
 /*--------------------------------------------------------------------
diff --git a/bin/varnishd/cache/cache_ws.c b/bin/varnishd/cache/cache_ws.c
index 4553c6abf..687d15d2d 100644
--- a/bin/varnishd/cache/cache_ws.c
+++ b/bin/varnishd/cache/cache_ws.c
@@ -361,16 +361,14 @@ void
 WS_VSB_new(struct vsb *vsb, struct ws *ws)
 {
 	unsigned u;
+	static char bogus[2];	// Smallest possible vsb
 
 	WS_Assert(ws);
 	u = WS_ReserveAll(ws);
-	if (WS_Overflowed(ws) || u < 2) {
-		/* Create a malloced-buffer VSB, and fail it up front */
-		AN(VSB_new(vsb, NULL, 2, 0));
-		VSB_cat(vsb, "XXX");
-	} else {
+	if (WS_Overflowed(ws) || u < 2)
+		AN(VSB_new(vsb, bogus, sizeof bogus, 0));
+	else
 		AN(VSB_new(vsb, WS_Front(ws), u, 0));
-	}
 }
 
 char *


More information about the varnish-commit mailing list