From a59a694ed0ff420551bdd54bb1bdb52937b0d9a6 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Mon, 11 Nov 2013 01:31:08 +0100
Subject: [PATCH] lib-sieve: deprecated notify extension: Fixed segfault
 problems in message string substitution.

---
 .../plugins/notify/ext-notify-common.c        | 12 ++---
 tests/deprecated/notify/basic.svtest          | 48 +++++++++++++++++++
 2 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/src/lib-sieve/plugins/notify/ext-notify-common.c b/src/lib-sieve/plugins/notify/ext-notify-common.c
index bd06dd457..a6b7001a1 100644
--- a/src/lib-sieve/plugins/notify/ext-notify-common.c
+++ b/src/lib-sieve/plugins/notify/ext-notify-common.c
@@ -243,14 +243,14 @@ void ext_notify_construct_message
 	/* Scan message for substitutions */
 	p = msg_format;
 	while ( *p != '\0' ) {
-		const char *const *header;
+		const char *header;
 
 		if ( strncasecmp(p, "$from$", 6) == 0 ) {
 			p += 6;
 
-			/* Fetch sender from oriinal message */
-			if ( mail_get_headers_utf8(msgdata->mail, "from", &header) >= 0 )
-				 str_append(out_msg, header[0]);
+			/* Fetch sender from original message */
+			if ( mail_get_first_header_utf8(msgdata->mail, "from", &header) > 0 )
+				 str_append(out_msg, header);
 
 		} else if ( strncasecmp(p, "$env-from$", 10) == 0 ) {
 			p += 10;
@@ -262,8 +262,8 @@ void ext_notify_construct_message
 			p += 9;
 
 			/* Fetch sender from oriinal message */
-			if ( mail_get_headers_utf8(msgdata->mail, "subject", &header) >= 0 )
-				 str_append(out_msg, header[0]);
+			if ( mail_get_first_header_utf8(msgdata->mail, "subject", &header) > 0 )
+				 str_append(out_msg, header);
 
 		} else if ( strncasecmp(p, "$text", 5) == 0
 			&& (p[5] == '[' || p[5] == '$') ) {
diff --git a/tests/deprecated/notify/basic.svtest b/tests/deprecated/notify/basic.svtest
index cd6b31d04..974f8ca5b 100644
--- a/tests/deprecated/notify/basic.svtest
+++ b/tests/deprecated/notify/basic.svtest
@@ -1,5 +1,6 @@
 require "vnd.dovecot.testsuite";
 require "notify";
+require "body";
 
 test "Execute" {
 	/* Test to catch runtime segfaults */
@@ -8,4 +9,51 @@ test "Execute" {
 		:low
 		:method "mailto"
 		:options ["stephan@example.com", "stephan@example.org"];
+
+	if not test_result_execute {
+		test_fail "Execute failed";
+	}
+}
+
+test_result_reset;
+
+test_set "message" text:
+To: user@example.com
+From: stephan@example.org
+Subject: Mail
+
+Test!
+.
+;
+
+test "Substitutions" {
+	notify
+		:message "$from$: $subject$"
+		:options "stephan@example.com";
+	if not test_result_execute {
+		test_fail "Execute failed";
+	}
+	test_message :smtp 0;
+	if not body :contains "stephan@example.org: Mail" {
+		test_fail "Substitution failed";
+	}
 }
+
+test_result_reset;
+
+test_set "message" text:
+To: user@example.com
+
+Test!
+.
+;
+
+test "Empty substitutions" {
+	notify
+		:message "$from$: $subject$"
+		:options "stephan@example.com";
+	if not test_result_execute {
+		test_fail "Execute failed";
+	}
+}
+
-- 
GitLab