[master] c7ea3dc90 vrt: Group hdr_t checks in new CHECK_HDR() macro

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Mon Apr 14 15:47:06 UTC 2025


commit c7ea3dc9039b555a6bcc46b89bdff079105a65b4
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Tue Apr 1 18:11:09 2025 +0200

    vrt: Group hdr_t checks in new CHECK_HDR() macro
    
    It's a clunky macro, but its current shape is only a transition towards
    something structured.

diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index f11263b2e..0cc17d572 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -482,10 +482,8 @@ http_IsHdr(const txt *hh, hdr_t hdr)
 	unsigned l;
 
 	Tcheck(*hh);
-	AN(hdr);
+	CHECK_HDR(hdr);
 	l = hdr[0];
-	assert(l == strlen(hdr + 1));
-	assert(hdr[l] == ':');
 	hdr++;
 	return (http_hdr_at(hdr, hh->b, l));
 }
@@ -556,6 +554,8 @@ http_CollectHdrSep(struct http *hp, hdr_t hdr, const char *sep)
 	const char *v;
 
 	CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
+	CHECK_HDR(hdr);
+
 	if (WS_Overflowed(hp->ws))
 		return;
 
@@ -564,8 +564,6 @@ http_CollectHdrSep(struct http *hp, hdr_t hdr, const char *sep)
 	lsep = strlen(sep);
 
 	l = hdr[0];
-	assert(l == strlen(hdr + 1));
-	assert(hdr[l] == ':');
 	f = http_findhdr(hp, l - 1, hdr + 1);
 	if (f == 0)
 		return;
@@ -638,9 +636,8 @@ http_GetHdr(const struct http *hp, hdr_t hdr, const char **ptr)
 	unsigned u, l;
 	const char *p;
 
+	CHECK_HDR(hdr);
 	l = hdr[0];
-	assert(l == strlen(hdr + 1));
-	assert(hdr[l] == ':');
 	hdr++;
 	u = http_findhdr(hp, l - 1, hdr);
 	if (u == 0) {
@@ -1373,12 +1370,9 @@ HTTP_GetHdrPack(struct worker *wrk, struct objcore *oc, hdr_t hdr)
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
 	CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
-	AN(hdr);
+	CHECK_HDR(hdr);
 
 	l = hdr[0];
-	assert(l > 0);
-	assert(l == strlen(hdr + 1));
-	assert(hdr[l] == ':');
 	hdr++;
 
 	if (hdr[0] == ':') {
diff --git a/include/vrt.h b/include/vrt.h
index 1a126e3e1..326611273 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -695,6 +695,15 @@ enum gethdr_e {
 
 typedef const char *hdr_t;
 
+#define CHECK_HDR(hdr)					\
+	do {						\
+		AN(hdr);				\
+		unsigned _l = hdr[0];			\
+		assert(_l > 0);				\
+		assert(_l == strlen(hdr + 1));		\
+		assert(hdr[_l] == ':');			\
+	} while (0)
+
 struct gethdr_s {
 	enum gethdr_e	where;
 	hdr_t		what;


More information about the varnish-commit mailing list