[master] 1fa6b74 use a vsb to return vcc_regexp's result

Nils Goroll nils.goroll at uplex.de
Tue May 8 15:55:08 UTC 2018


commit 1fa6b742df0a1bec4185e05f45a36b11ad75ece1
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Tue May 8 17:50:33 2018 +0200

    use a vsb to return vcc_regexp's result
    
    to avoid micro-managing memory
    
    Follow up cf20e04efa007be1632c9f137338203f9691d25a

diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index f530095..fef82ec 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -322,7 +322,7 @@ void vcc_Parse_Init(struct vcc *);
 sym_act_f vcc_Act_If;
 
 /* vcc_utils.c */
-const char *vcc_regexp(struct vcc *tl);
+void vcc_regexp(struct vcc *tl, struct vsb *vgc_name);
 void Resolve_Sockaddr(struct vcc *tl, const char *host, const char *defport,
     const char **ipv4, const char **ipv4_ascii, const char **ipv6,
     const char **ipv6_ascii, const char **p_ascii, int maxips,
diff --git a/lib/libvcc/vcc_expr.c b/lib/libvcc/vcc_expr.c
index b0efed5..37bfb04 100644
--- a/lib/libvcc/vcc_expr.c
+++ b/lib/libvcc/vcc_expr.c
@@ -288,8 +288,8 @@ vcc_Eval_Regsub(struct vcc *tl, struct expr **e, struct token *t,
 {
 	struct expr *e2;
 	int all = sym->eval_priv == NULL ? 0 : 1;
-	const char *p;
 	char buf[128];
+	struct vsb vsb;
 
 	(void)t;
 	(void)fmt;
@@ -298,10 +298,13 @@ vcc_Eval_Regsub(struct vcc *tl, struct expr **e, struct token *t,
 	ERRCHK(tl);
 	SkipToken(tl, ',');
 	ExpectErr(tl, CSTR);
-	p = vcc_regexp(tl);
-	bprintf(buf, "VRT_regsub(ctx, %d,\v+\n\v1,\n%s", all, p);
-	free(TRUST_ME(p));
-	*e = vcc_expr_edit(tl, STRING, buf, e2, NULL);
+
+	AN(VSB_new(&vsb, buf, sizeof buf, VSB_FIXEDLEN));
+	VSB_printf(&vsb, "VRT_regsub(ctx, %d,\v+\n\v1,\n", all);
+	vcc_regexp(tl, &vsb);
+	ERRCHK(tl);
+	AZ(VSB_finish(&vsb));
+	*e = vcc_expr_edit(tl, STRING, VSB_data(&vsb), e2, NULL);
 	SkipToken(tl, ',');
 	vcc_expr0(tl, &e2, STRING);
 	ERRCHK(tl);
@@ -983,16 +986,18 @@ static void v_matchproto_(cmp_f)
 cmp_regexp(struct vcc *tl, struct expr **e, const struct cmps *cp)
 {
 	char buf[128];
-	const char *re;
+	struct vsb vsb;
 
 	*e = vcc_expr_edit(tl, STRING, "\vS", *e, NULL);
 	vcc_NextToken(tl);
 	ExpectErr(tl, CSTR);
-	re = vcc_regexp(tl);
+	AN(VSB_new(&vsb, buf, sizeof buf, VSB_FIXEDLEN));
+	VSB_printf(&vsb, "%sVRT_re_match(ctx, \v1, ", cp->emit);
+	vcc_regexp(tl, &vsb);
 	ERRCHK(tl);
-	bprintf(buf, "%sVRT_re_match(ctx, \v1, %s)", cp->emit, re);
-	free(TRUST_ME(re));
-	*e = vcc_expr_edit(tl, BOOL, buf, *e, NULL);
+	VSB_cat(&vsb, ")");
+	AZ(VSB_finish(&vsb));
+	*e = vcc_expr_edit(tl, BOOL, VSB_data(&vsb), *e, NULL);
 }
 
 static void v_matchproto_(cmp_f)
diff --git a/lib/libvcc/vcc_utils.c b/lib/libvcc/vcc_utils.c
index 33e2aa6..71db6a7 100644
--- a/lib/libvcc/vcc_utils.c
+++ b/lib/libvcc/vcc_utils.c
@@ -47,10 +47,10 @@
 
 /*--------------------------------------------------------------------*/
 
-const char *
-vcc_regexp(struct vcc *tl)
+void
+vcc_regexp(struct vcc *tl, struct vsb *vgc_name)
 {
-	char buf[BUFSIZ], *p;
+	char buf[BUFSIZ];
 	vre_t *t;
 	const char *error;
 	int erroroffset;
@@ -58,18 +58,18 @@ vcc_regexp(struct vcc *tl)
 
 	Expect(tl, CSTR);
 	if (tl->err)
-		return (NULL);
+		return;
 	t = VRE_compile(tl->t->dec, 0, &error, &erroroffset);
 	if (t == NULL) {
 		VSB_printf(tl->sb,
 		    "Regexp compilation error:\n\n%s\n\n", error);
 		vcc_ErrWhere(tl, tl->t);
-		return (NULL);
+		return;
 	}
 	VRE_free(&t);
 	bprintf(buf, "VGC_re_%u", tl->unique++);
-	p = TlAlloc(tl, strlen(buf) + 1);
-	strcpy(p, buf);
+	if (vgc_name)
+		VSB_cat(vgc_name, buf);
 
 	Fh(tl, 0, "static void *%s;\n", buf);
 	ifp = New_IniFin(tl);
@@ -78,7 +78,6 @@ vcc_regexp(struct vcc *tl)
 	VSB_printf(ifp->ini, ");");
 	VSB_printf(ifp->fin, "\t\tVRT_re_fini(%s);", buf);
 	vcc_NextToken(tl);
-	return (p);
 }
 
 /*


More information about the varnish-commit mailing list