[master] 4fe2ad4 Add a WS_Printf() function and use it.
Poul-Henning Kamp
phk at FreeBSD.org
Tue Jan 28 21:11:45 CET 2014
commit 4fe2ad4a339bcb89980f4859e97b05006d13b4cd
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Tue Jan 28 20:11:20 2014 +0000
Add a WS_Printf() function and use it.
Other minor polish around workspaces.
diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 5e0dcd2..6e522bb 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -1197,6 +1197,7 @@ char *WS_Alloc(struct ws *ws, unsigned bytes);
void *WS_Copy(struct ws *ws, const void *str, int len);
char *WS_Snapshot(struct ws *ws);
int WS_Overflowed(const struct ws *ws);
+void *WS_Printf(struct ws *ws, const char *fmt, ...) __printflike(2, 3);
/* rfc2616.c */
void RFC2616_Ttl(struct busyobj *);
diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index 6c3abbc..ec28396 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -718,20 +718,17 @@ static void
http_PutField(const struct http *to, int field, const char *string)
{
char *p;
- unsigned l;
CHECK_OBJ_NOTNULL(to, HTTP_MAGIC);
- l = strlen(string);
- p = WS_Alloc(to->ws, l + 1);
+ p = WS_Copy(to->ws, string, -1);
if (p == NULL) {
VSLb(to->vsl, SLT_LostHeader, "%s", string);
to->hd[field].b = NULL;
to->hd[field].e = NULL;
to->hdf[field] = 0;
} else {
- memcpy(p, string, l + 1L);
to->hd[field].b = p;
- to->hd[field].e = p + l;
+ to->hd[field].e = strchr(p, '\0');
to->hdf[field] = 0;
http_VSLH(to, field);
}
diff --git a/bin/varnishd/cache/cache_http1_proto.c b/bin/varnishd/cache/cache_http1_proto.c
index 8d61a64..a896546 100644
--- a/bin/varnishd/cache/cache_http1_proto.c
+++ b/bin/varnishd/cache/cache_http1_proto.c
@@ -517,6 +517,7 @@ HTTP1_Write(const struct worker *w, const struct http *hp, int resp)
hp->hd[HTTP_HDR_STATUS].b = WS_Alloc(hp->ws, 4);
AN(hp->hd[HTTP_HDR_STATUS].b);
+ assert(hp->status >= 100 && hp->status <= 999);
sprintf(hp->hd[HTTP_HDR_STATUS].b, "%3d", hp->status);
hp->hd[HTTP_HDR_STATUS].e = hp->hd[HTTP_HDR_STATUS].b + 3;
diff --git a/bin/varnishd/cache/cache_panic.c b/bin/varnishd/cache/cache_panic.c
index 3cfb36f..81b794d 100644
--- a/bin/varnishd/cache/cache_panic.c
+++ b/bin/varnishd/cache/cache_panic.c
@@ -109,7 +109,10 @@ pan_ws(const struct ws *ws, int indent)
{
VSB_printf(pan_vsp, "%*sws = %p {", indent, "", ws);
- if (VALID_OBJ(ws, WS_MAGIC)) {
+ if (!VALID_OBJ(ws, WS_MAGIC)) {
+ if (ws != NULL)
+ VSB_printf(pan_vsp, " BAD_MAGIC(0x%08x) ", ws->magic);
+ } else {
if (WS_Overflowed(ws))
VSB_printf(pan_vsp, " OVERFLOW");
VSB_printf(pan_vsp,
@@ -128,8 +131,6 @@ pan_ws(const struct ws *ws, int indent)
VSB_printf(pan_vsp, ",+%ld", (long) (ws->e - ws->s));
else
VSB_printf(pan_vsp, ",%p", ws->e);
- } else {
- VSB_printf(pan_vsp, " BAD_MAGIC(0x%08x) ", ws->magic);
}
VSB_printf(pan_vsp, "},\n");
VSB_printf(pan_vsp, "%*s},\n", indent, "" );
diff --git a/bin/varnishd/cache/cache_vrt.c b/bin/varnishd/cache/cache_vrt.c
index 7138da5..3d45e4c 100644
--- a/bin/varnishd/cache/cache_vrt.c
+++ b/bin/varnishd/cache/cache_vrt.c
@@ -313,27 +313,17 @@ VRT_IP_string(const struct vrt_ctx *ctx, VCL_IP ip)
char *
VRT_INT_string(const struct vrt_ctx *ctx, long num)
{
- char *p;
- int size;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
- size = snprintf(NULL, 0, "%ld", num) + 1;
- AN(p = WS_Alloc(ctx->ws, size));
- assert(snprintf(p, size, "%ld", num) < size);
- return (p);
+ return (WS_Printf(ctx->ws, "%ld", num));
}
char *
VRT_REAL_string(const struct vrt_ctx *ctx, double num)
{
- char *p;
- int size;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
- size = snprintf(NULL, 0, "%.3f", num) + 1;
- AN(p = WS_Alloc(ctx->ws, size));
- assert(snprintf(p, size, "%.3f", num) < size);
- return (p);
+ return (WS_Printf(ctx->ws, "%.3f", num));
}
char *
diff --git a/bin/varnishd/cache/cache_vrt_var.c b/bin/varnishd/cache/cache_vrt_var.c
index fb2d481..dad35f3 100644
--- a/bin/varnishd/cache/cache_vrt_var.c
+++ b/bin/varnishd/cache/cache_vrt_var.c
@@ -471,16 +471,12 @@ VRT_DO_EXP(beresp, ctx->bo->exp, keep, 0, ctx->bo->exp.t_origin,)
const char *
VRT_r_req_xid(const struct vrt_ctx *ctx)
{
- char *p;
- int size;
+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
- // XXX ?
- size = snprintf(NULL, 0, "%u", ctx->req->vsl->wid & VSL_IDENTMASK) + 1;
- AN(p = WS_Alloc(ctx->req->http->ws, size));
- assert(snprintf(p, size, "%u", ctx->req->vsl->wid & VSL_IDENTMASK) < size);
- return (p);
+ return (WS_Printf(ctx->req->http->ws, "%u",
+ ctx->req->vsl->wid & VSL_IDENTMASK));
}
/*--------------------------------------------------------------------*/
diff --git a/bin/varnishd/cache/cache_ws.c b/bin/varnishd/cache/cache_ws.c
index 0ee8124..5ed9040 100644
--- a/bin/varnishd/cache/cache_ws.c
+++ b/bin/varnishd/cache/cache_ws.c
@@ -30,6 +30,9 @@
#include "config.h"
+#include <stdio.h>
+#include <stdarg.h>
+
#include "cache.h"
void
@@ -159,6 +162,28 @@ WS_Copy(struct ws *ws, const void *str, int len)
return (r);
}
+void *
+WS_Printf(struct ws *ws, const char *fmt, ...)
+{
+ unsigned u, v;
+ va_list ap;
+ char *p;
+
+ WS_Assert(ws);
+ assert(ws->r == NULL);
+ u = WS_Reserve(ws, 0);
+ p = ws->f;
+ va_start(ap, fmt);
+ v = vsnprintf(p, u, fmt, ap);
+ if (v > u) {
+ WS_Release(ws, 0);
+ p = NULL;
+ } else {
+ WS_Release(ws, v);
+ }
+ return (p);
+}
+
char *
WS_Snapshot(struct ws *ws)
{
More information about the varnish-commit
mailing list