[master] c0fd77a Ensure valid C symbols for both headers and vars

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Mon Aug 14 11:02:06 CEST 2017


commit c0fd77a0664e2abbe90f781cc7d363e4919b1dc9
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Wed Jun 28 12:46:38 2017 +0200

    Ensure valid C symbols for both headers and vars
    
    Refs #2325

diff --git a/lib/libvcc/vcc_acl.c b/lib/libvcc/vcc_acl.c
index b97ad2c..f6acc44 100644
--- a/lib/libvcc/vcc_acl.c
+++ b/lib/libvcc/vcc_acl.c
@@ -342,31 +342,36 @@ vcc_acl_entry(struct vcc *tl)
  */
 
 static void
-vcc_acl_emit(struct vcc *tl, const char *acln, int anon)
+vcc_acl_emit(struct vcc *tl, const char *name, const char *rname, int anon)
 {
 	struct acl_e *ae;
 	int depth, l, m, i;
 	unsigned at[VRT_ACL_MAXADDR + 1];
 	struct token *t;
 	struct inifin *ifp;
+	struct vsb *func;
+
+	func = VSB_new_auto();
+	AN(func);
+	VSB_printf(func, "match_acl_%s_", anon ? "anon" : "named");
+	VCC_PrintCName(func, name, NULL);
+	AZ(VSB_finish(func));
 
 	Fh(tl, 0, "\nstatic int __match_proto__(acl_match_f)\n");
-	Fh(tl, 0,
-	    "match_acl_%s_%s(VRT_CTX, const VCL_IP p)\n",
-	    anon ? "anon" : "named", acln); /* XXX: acln isn't an rname */
+	Fh(tl, 0, "%s(VRT_CTX, const VCL_IP p)\n", VSB_data(func));
 	Fh(tl, 0, "{\n");
 	Fh(tl, 0, "\tconst unsigned char *a;\n");
 	Fh(tl, 0, "\tint fam;\n");
 	Fh(tl, 0, "\n");
 	Fh(tl, 0, "\tfam = VRT_VSA_GetPtr(p, &a);\n");
 	Fh(tl, 0, "\tif (fam < 0) {\n");
-	Fh(tl, 0, "\t\tVRT_acl_log(ctx, \"NO_FAM %s\");\n", acln);
+	Fh(tl, 0, "\t\tVRT_acl_log(ctx, \"NO_FAM %s\");\n", name);
 	Fh(tl, 0, "\t\treturn(0);\n");
 	Fh(tl, 0, "\t}\n\n");
 	if (!tl->err_unref && !anon) {
 		ifp = New_IniFin(tl);
 		VSB_printf(ifp->ini,
-			"\tif (0) match_acl_named_%s(0, 0);\n", acln);
+			"\tif (0) %s(0, 0);\n", VSB_data(func));
 	}
 	depth = -1;
 	at[0] = 256;
@@ -414,7 +419,7 @@ vcc_acl_emit(struct vcc *tl, const char *acln, int anon)
 
 		if (!anon) {
 			Fh(tl, 0, "\t%*sVRT_acl_log(ctx, \"%sMATCH %s \" ",
-			    -i, "", ae->not ? "NEG_" : "", acln);
+			    -i, "", ae->not ? "NEG_" : "", name);
 			t = ae->t_addr;
 			do {
 				if (t->tok == CSTR) {
@@ -440,33 +445,34 @@ vcc_acl_emit(struct vcc *tl, const char *acln, int anon)
 
 	/* Deny by default */
 	if (!anon)
-		Fh(tl, 0, "\tVRT_acl_log(ctx, \"NO_MATCH %s\");\n", acln);
+		Fh(tl, 0, "\tVRT_acl_log(ctx, \"NO_MATCH %s\");\n", name);
 	Fh(tl, 0, "\treturn (0);\n}\n");
 
 	if (anon)
 		return;
 
 	/* Emit the struct that will be referenced */
-	Fh(tl, 0, "\nconst struct vrt_acl vrt_acl_named_%s[] = {{\n", acln);
+	Fh(tl, 0, "\nconst struct vrt_acl %s[] = {{\n", rname);
 	Fh(tl, 0, "\t.magic = VRT_ACL_MAGIC,\n");
-	Fh(tl, 0, "\t.match = &match_acl_named_%s,\n", acln);
+	Fh(tl, 0, "\t.match = &%s,\n", VSB_data(func));
 	Fh(tl, 0, "}};\n\n");
+	VSB_destroy(&func);
 }
 
 void
 vcc_Acl_Hack(struct vcc *tl, char *b, size_t bl)
 {
-	char acln[32];
+	char name[32];
 	unsigned tcond;
 
 	VTAILQ_INIT(&tl->acl);
 	tcond = tl->t->tok;
 	vcc_NextToken(tl);
-	bprintf(acln, "%u", tl->unique++);
+	bprintf(name, "%u", tl->unique++);
 	vcc_acl_entry(tl);
-	vcc_acl_emit(tl, acln, 1);
+	vcc_acl_emit(tl, name, name, 1);
 	assert(snprintf(b, bl - 1, "%smatch_acl_anon_%s(ctx, \v1)",
-	    (tcond == T_NEQ ? "!" : ""), acln) < bl - 1);
+	    (tcond == T_NEQ ? "!" : ""), name) < bl - 1);
 }
 
 void
@@ -496,5 +502,5 @@ vcc_ParseAcl(struct vcc *tl)
 	}
 	SkipToken(tl, '}');
 
-	vcc_acl_emit(tl, sym->name, 0);
+	vcc_acl_emit(tl, sym->name, sym->rname, 0);
 }
diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index 6859df2..6826e1e 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -300,6 +300,7 @@ void Resolve_Sockaddr(struct vcc *tl, const char *host, const char *defport,
 void vcc_stevedore(struct vcc *vcc, const char *stv_name);
 
 /* vcc_symb.c */
+void VCC_PrintCName(struct vsb *vsb, const char *b, const char *e);
 struct symbol *VCC_Symbol(struct vcc *, struct symbol *,
     const char *, const char *, enum symkind, int);
 #define VCC_SymbolTok(vcc, sym, tok, kind, create) \
diff --git a/lib/libvcc/vcc_symb.c b/lib/libvcc/vcc_symb.c
index 8fc56dc..513f4c0 100644
--- a/lib/libvcc/vcc_symb.c
+++ b/lib/libvcc/vcc_symb.c
@@ -36,6 +36,8 @@
 
 #include "vcc_compile.h"
 
+#include "vct.h"
+
 /*--------------------------------------------------------------------*/
 
 enum symkind
@@ -63,6 +65,24 @@ VCC_SymKind(struct vcc *tl, const struct symbol *s)
 	}
 }
 
+void
+VCC_PrintCName(struct vsb *vsb, const char *b, const char *e)
+{
+
+	AN(vsb);
+	AN(b);
+
+	if (e == NULL)
+		e = strchr(b, '\0');
+	assert(b < e);
+
+	for (; b < e; b++)
+		if (vct_isalnum(*b))
+			VSB_putc(vsb, *b);
+		else
+			VSB_printf(vsb, "_%02x_", *b);
+}
+
 static struct symbol *
 vcc_new_symbol(struct vcc *tl, const char *b, const char *e)
 {
@@ -189,7 +209,8 @@ VCC_GlobalSymbol(struct symbol *sym, vcc_type_t fmt, const char *pfx)
 
 	vsb = VSB_new_auto();
 	AN(vsb);
-	VSB_printf(vsb, "%s_%s", pfx, sym->name);
+	VSB_printf(vsb, "%s_", pfx);
+	VCC_PrintCName(vsb, sym->name, NULL);
 	AZ(VSB_finish(vsb));
 	REPLACE(sym->rname, VSB_data(vsb));
 	AN(sym->rname);
diff --git a/lib/libvcc/vcc_var.c b/lib/libvcc/vcc_var.c
index 50fa9e9..4e69db2 100644
--- a/lib/libvcc/vcc_var.c
+++ b/lib/libvcc/vcc_var.c
@@ -44,9 +44,8 @@ vcc_Var_Wildcard(struct vcc *tl, struct symbol *parent,
 	struct symbol *sym;
 	struct var *v;
 	const struct var *vh;
-	unsigned u;
-	const char *p;
 	struct vsb *vsb;
+	unsigned len;
 
 	vh = parent->wildcard_priv;
 	assert(vh->fmt == HEADER);
@@ -68,17 +67,14 @@ vcc_Var_Wildcard(struct vcc *tl, struct symbol *parent,
 	vsb = VSB_new_auto();
 	AN(vsb);
 	VSB_printf(vsb, "&VGC_%s_", vh->rname);
-	for (p = b, u = 1; p < e; p++, u++)
-		if (vct_isalnum(*p))
-			VSB_putc(vsb, *p);
-		else
-			VSB_printf(vsb, "_%02x_", *p);
+	VCC_PrintCName(vsb, b, e);
 	AZ(VSB_finish(vsb));
 
 	/* Create the static identifier */
+	len = (unsigned)(e - b);
 	Fh(tl, 0, "static const struct gethdr_s %s =\n", VSB_data(vsb) + 1);
 	Fh(tl, 0, "    { %s, \"\\%03o%.*s:\"};\n",
-	    vh->rname, u, (int)(e - b), b);
+	    vh->rname, len + 1, len, b);
 
 	/* Create the symbol r/l values */
 	v->rname = TlDup(tl, VSB_data(vsb));



More information about the varnish-commit mailing list