[master] a88dbf9 Fix a memory leak in BAN processing: We didn't free the compiled regexp.

Poul-Henning Kamp phk at FreeBSD.org
Tue Feb 11 11:33:14 CET 2014


commit a88dbf913c7c264bb994f1fd5810e8a6ab27c3cc
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Feb 11 10:32:01 2014 +0000

    Fix a memory leak in BAN processing:  We didn't free the compiled regexp.
    
    Also solve a abstract corner-case when malloc(3) fails.  We'll probably
    still come crashing down, but not for this reason any more.

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 93ec51f..7ab3a1a 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -811,6 +811,7 @@ struct ban *BAN_New(void);
 int BAN_AddTest(struct ban *, const char *, const char *, const char *);
 void BAN_Free(struct ban *b);
 char *BAN_Insert(struct ban *b);
+void BAN_Free_Errormsg(char *);
 void BAN_Init(void);
 void BAN_Shutdown(void);
 void BAN_NewObjCore(struct objcore *oc);
diff --git a/bin/varnishd/cache/cache_ban.c b/bin/varnishd/cache/cache_ban.c
index 1c6b0c8..99f2914 100644
--- a/bin/varnishd/cache/cache_ban.c
+++ b/bin/varnishd/cache/cache_ban.c
@@ -394,6 +394,7 @@ ban_parse_regexp(struct ban *b, const char *a3)
 	rc = pcre_fullinfo(re, NULL, PCRE_INFO_SIZE, &sz);
 	AZ(rc);
 	ban_add_lump(b, re, sz);
+	pcre_free(re);
 	return (0);
 }
 
@@ -464,19 +465,27 @@ BAN_AddTest(struct ban *b, const char *a1, const char *a2, const char *a3)
  *      deleted.
  */
 
+static char ban_error_nomem[] = "Could not get memory";
+
 static char *
 ban_ins_error(const char *p)
 {
 	char *r = NULL;
-	static char nomem[] = "Could not get memory";
 
 	if (p != NULL)
 		r = strdup(p);
 	if (r == NULL)
-		r = nomem;
+		r = ban_error_nomem;
 	return (r);
 }
 
+void
+BAN_Free_Errormsg(char *p)
+{
+	if (p != ban_error_nomem)
+		free(p);
+}
+
 char *
 BAN_Insert(struct ban *b)
 {
@@ -1229,7 +1238,7 @@ ccf_ban(struct cli *cli, const char * const *av, void *priv)
 	p = BAN_Insert(b);
 	if (p != NULL) {
 		VCLI_Out(cli, "%s", p);
-		free(p);
+		BAN_Free_Errormsg(p);
 		VCLI_SetResult(cli, CLIS_PARAM);
 	}
 }
diff --git a/bin/varnishd/cache/cache_vrt.c b/bin/varnishd/cache/cache_vrt.c
index 3d45e4c..ce536e5 100644
--- a/bin/varnishd/cache/cache_vrt.c
+++ b/bin/varnishd/cache/cache_vrt.c
@@ -446,7 +446,7 @@ VRT_ban_string(const struct vrt_ctx *ctx, const char *str)
 			if (a1 != NULL) {
 				VSLb(ctx->vsl, SLT_VCL_Error,
 				    "ban(): %s", a1);
-				free(a1);
+				BAN_Free_Errormsg(a1);
 			}
 			break;
 		}



More information about the varnish-commit mailing list