From 2ac180275d5235472f59bb9072dabddeff51a489 Mon Sep 17 00:00:00 2001
From: Matthew Nicholson <mnicholson@digium.com>
Date: Fri, 8 Jul 2011 19:54:23 +0000
Subject: [PATCH] Merged revisions 327106 via svnmerge from
 https://origsvn.digium.com/svn/asterisk/branches/1.8

........
  r327106 | mnicholson | 2011-07-08 14:52:51 -0500 (Fri, 08 Jul 2011) | 11 lines

  Reset our ast_str before passing it on to dialplan function backends.

  It is possible for a dialplan backend to not modify the given buffer or ast_str
  and still return success. This causes any previous value stored in the buffer
  to be used as if the new function call provided it. Some functions also append
  to the given buffer assuming it is empty.

  The test_substitution unit test has also been modified to detect this problem.

  (closes issue ASTERISK-17878)
........


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@327107 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 main/pbx.c                | 2 +-
 tests/test_substitution.c | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/main/pbx.c b/main/pbx.c
index 0059cbd0b5..3cb9affebd 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -3663,9 +3663,9 @@ int ast_func_read2(struct ast_channel *chan, const char *function, struct ast_st
 		if (acfptr->mod) {
 			u = __ast_module_user_add(acfptr->mod, chan);
 		}
+		ast_str_reset(*str);
 		if (acfptr->read2) {
 			/* ast_str enabled */
-			ast_str_reset(*str);
 			res = acfptr->read2(chan, copy, args, str, maxlen);
 		} else {
 			/* Legacy function pointer, allocate buffer for result */
diff --git a/tests/test_substitution.c b/tests/test_substitution.c
index aa7d14f914..98e1ed1911 100644
--- a/tests/test_substitution.c
+++ b/tests/test_substitution.c
@@ -261,6 +261,8 @@ AST_TEST_DEFINE(test_substitution)
 	TEST(test_expected_result(test, c, "A${${baz}o:-2:-1}A", "A2A"));
 	pbx_builtin_setvar_helper(c, "list1", "ab&cd&ef");
 	TEST(test_expected_result(test, c, "${LISTFILTER(list1,&,cd)}", "ab&ef"));
+	TEST(test_expected_result(test, c, "${SHELL(echo -n 123)},${SHELL(echo -n 456)}", "123,456"));
+	TEST(test_expected_result(test, c, "${foo},${CDR(answer)},${SHELL(echo -n 456)}", "123,,456"));
 #undef TEST
 
 	/* For testing dialplan functions */
-- 
GitLab