From 0c1b149c24ad7a4aed70d6f4585e455fa5778315 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Thu, 20 Nov 2008 00:06:58 +0100
Subject: [PATCH] Enabled (optional) support for Valgrind in the testsuite and
 fixed a few intricate bugs in the process.

---
 Makefile.am                                        | 10 ++++++++--
 configure.in                                       | 10 ++++++++++
 src/lib-sieve/plugins/include/ext-include-binary.c |  9 ++++-----
 src/lib-sieve/plugins/include/ext-include-common.c |  3 ++-
 src/lib-sieve/plugins/include/ext-include.c        | 13 +------------
 src/lib-sieve/sieve-binary.c                       |  9 ++++++---
 src/lib-sieve/sieve-binary.h                       |  4 ++--
 7 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index e546a1504..1395020c8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -27,6 +27,12 @@ endif
 
 TESTSUITE_BIN = $(top_srcdir)/src/testsuite/testsuite
 
+if TESTSUITE_VALGRIND
+TEST_BIN = valgrind -q --error-exitcode=1 --leak-check=full $(TESTSUITE_BIN) 
+else
+TEST_BIN = $(TESTSUITE_BIN)
+endif
+
 test_cases = \
 	tests/testsuite.svtest\
 	tests/control-structures.svtest \
@@ -52,8 +58,8 @@ test_cases = \
 	tests/extensions/variables/string.svtest \
 	tests/extensions/variables/errors.svtest \
 	tests/extensions/variables/regex.svtest \
-	tests/extensions/include/variables.svtest \
 	tests/extensions/include/errors.svtest \
+	tests/extensions/include/variables.svtest \
 	tests/extensions/imapflags/basic.svtest \
 	tests/extensions/imapflags/hasflag.svtest \
 	tests/extensions/imapflags/execute.svtest \
@@ -74,7 +80,7 @@ test_cases = \
 if HAVE_DOVECOT_LIBS
 
 $(test_cases):
-	@$(TESTSUITE_BIN) $@
+	@$(TEST_BIN) $@
 
 .PHONY: $(test_cases)
 
diff --git a/configure.in b/configure.in
index 5e39a346f..d84b67507 100644
--- a/configure.in
+++ b/configure.in
@@ -69,6 +69,16 @@ fi
 #
 #
 
+AC_ARG_ENABLE(valgrind,
+[AC_HELP_STRING([--enable-valgrind], [Enable Valgrind memory leak checks in testsuite [default=no]])],
+    if test x$enableval = xno || test x$enableval = xauto; then
+        want_valgrind=$enableval
+    else
+        want_valgrind=yes
+    fi,
+    want_valgrind=no)
+AM_CONDITIONAL(TESTSUITE_VALGRIND, test "$want_valgrind" = "yes")
+
 dnl replace relative ../ paths in the file with full paths
 eval `cat $dovecotdir/dovecot-config|sed 's,\$(top_builddir)/,$dovecotdir/,g'`
 
diff --git a/src/lib-sieve/plugins/include/ext-include-binary.c b/src/lib-sieve/plugins/include/ext-include-binary.c
index fc5525ef4..a722e20e1 100644
--- a/src/lib-sieve/plugins/include/ext-include-binary.c
+++ b/src/lib-sieve/plugins/include/ext-include-binary.c
@@ -68,7 +68,9 @@ static struct ext_include_binary_context *ext_include_binary_create_context
 		(hash_callback_t *) sieve_script_hash, 
 		(hash_cmp_callback_t *) sieve_script_cmp);
 	p_array_init(&ctx->include_index, pool, 128);
-	
+
+	sieve_binary_extension_set(sbin, &include_binary_ext, ctx);
+
 	return ctx;
 }
 
@@ -78,10 +80,8 @@ struct ext_include_binary_context *ext_include_binary_get_context
 	struct ext_include_binary_context *ctx = (struct ext_include_binary_context *)
 		sieve_binary_extension_get_context(sbin, &include_extension);
 	
-	if ( ctx == NULL ) {
+	if ( ctx == NULL )
 		ctx = ext_include_binary_create_context(sbin);
-		sieve_binary_extension_set_context(sbin, &include_extension, ctx);
-	};
 	
 	return ctx;
 }
@@ -344,7 +344,6 @@ static void ext_include_binary_free(struct sieve_binary *sbin)
 
 	if ( binctx->global_vars != NULL ) 
 		sieve_variable_scope_unref(&binctx->global_vars);
-
 }
 
 /*
diff --git a/src/lib-sieve/plugins/include/ext-include-common.c b/src/lib-sieve/plugins/include/ext-include-common.c
index 621e36918..63cee40fa 100644
--- a/src/lib-sieve/plugins/include/ext-include-common.c
+++ b/src/lib-sieve/plugins/include/ext-include-common.c
@@ -586,9 +586,10 @@ bool ext_include_execute_include
 		/* Free any sub-interpreters that might still be active */
 		while ( curctx != NULL && curctx->parent != NULL ) {
 			struct ext_include_interpreter_context *nextctx	= curctx->parent;
+			struct sieve_interpreter *killed_interp = curctx->interp;
 
 			/* This kills curctx too */
-			sieve_interpreter_free(&curctx->interp);
+			sieve_interpreter_free(&killed_interp);
 
 			/* Luckily we recorded the parent earlier */
 			curctx = nextctx;
diff --git a/src/lib-sieve/plugins/include/ext-include.c b/src/lib-sieve/plugins/include/ext-include.c
index 58f3f63ad..20c4b7213 100644
--- a/src/lib-sieve/plugins/include/ext-include.c
+++ b/src/lib-sieve/plugins/include/ext-include.c
@@ -50,7 +50,6 @@ static const struct sieve_operation *ext_include_operations[] = {
 static bool ext_include_load(int ext_id);
 static bool ext_include_validator_load(struct sieve_validator *validator);
 static bool ext_include_generator_load(const struct sieve_codegen_env *cgenv);
-static bool ext_include_binary_load(struct sieve_binary *sbin);
 static bool ext_include_interpreter_load
 	(const struct sieve_runtime_env *renv, sieve_size_t *address);
 
@@ -65,7 +64,7 @@ const struct sieve_extension include_extension = {
 	ext_include_validator_load, 
 	ext_include_generator_load,
 	ext_include_interpreter_load,
-	ext_include_binary_load, 
+	NULL,
 	ext_include_binary_dump,
 	ext_include_code_dump,
 	SIEVE_EXT_DEFINE_OPERATIONS(ext_include_operations),
@@ -107,13 +106,3 @@ static bool ext_include_interpreter_load
 	return TRUE;
 }
 
-static bool ext_include_binary_load(struct sieve_binary *sbin)
-{
-	/* Register extension to the binary object to get notified of events like 
-	 * opening or saving the binary. The implemententation of these hooks is found
-	 * in ext-include-binary.c
-	 */
-	sieve_binary_extension_set(sbin, &include_extension, &include_binary_ext);
-	
-	return TRUE;
-}
diff --git a/src/lib-sieve/sieve-binary.c b/src/lib-sieve/sieve-binary.c
index 3c79a089c..133f4a899 100644
--- a/src/lib-sieve/sieve-binary.c
+++ b/src/lib-sieve/sieve-binary.c
@@ -1289,13 +1289,16 @@ const void *sieve_binary_extension_get_context
 }
 
 void sieve_binary_extension_set
-(struct sieve_binary *sbin, const struct sieve_extension *ext, 
-	const struct sieve_binary_extension *bext)
+(struct sieve_binary *sbin, const struct sieve_binary_extension *bext,
+	void *context)
 {
 	struct sieve_binary_extension_reg *ereg = 
-		sieve_binary_extension_get_reg(sbin, ext, TRUE);
+		sieve_binary_extension_get_reg(sbin, bext->extension, TRUE);
 	
 	ereg->binext = bext;
+
+	if ( context != NULL )
+		ereg->context = context;
 }
 
 unsigned int sieve_binary_extension_create_block
diff --git a/src/lib-sieve/sieve-binary.h b/src/lib-sieve/sieve-binary.h
index d934c86ca..edb0e0f17 100644
--- a/src/lib-sieve/sieve-binary.h
+++ b/src/lib-sieve/sieve-binary.h
@@ -83,8 +83,8 @@ const void *sieve_binary_extension_get_context
 	(struct sieve_binary *sbin, const struct sieve_extension *ext);
 	
 void sieve_binary_extension_set
-	(struct sieve_binary *sbin, const struct sieve_extension *ext, 
-		const struct sieve_binary_extension *bext);
+	(struct sieve_binary *sbin, const struct sieve_binary_extension *bext,
+		void *context);
 
 unsigned int sieve_binary_extension_create_block
 	(struct sieve_binary *sbin, const struct sieve_extension *ext);
-- 
GitLab