Commit 1af2909bb6 for freeswitch.com

commit 1af2909bb6772aa606f1deb4bde2d858e279f4c4
Author: Andrey Volk <andywolk@gmail.com>
Date:   Tue Nov 17 15:35:58 2026 +0300

    [mod_dialplan_xml] Fix $N substitution of <regex> captures (#3293)

    Keep the subject string of a saved match alive and substitute only when a regex matched. Add unit-tests.

diff --git a/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c b/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c
index cab522887c..4c9687d4ab 100644
--- a/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c
+++ b/src/mod/dialplans/mod_dialplan_xml/mod_dialplan_xml.c
@@ -339,7 +339,7 @@ static int parse_exten(switch_core_session_t *session, switch_caller_profile_t *
 					if (all) break;
 				}

-				if (field && strchr(expression, '(')) {
+				if (field && match_data && strchr(expression, '(')) {
 					char var[256];
 					switch_snprintf(var, sizeof(var), "DP_REGEX_MATCH_%d", total);

@@ -352,7 +352,14 @@ static int parse_exten(switch_core_session_t *session, switch_caller_profile_t *
 					switch_regex_safe_free(save_re);

 					save_expression = strdup(expression);
-					save_field_data = strdup(field_data);
+					/* match_data points into the subject string it matched, so field_expanded moves with it */
+					if (field_expanded) {
+						save_field_data = field_expanded;
+						field_expanded = NULL;
+					} else {
+						save_field_data = strdup(field_data);
+					}
+
 					save_re = re;
 					save_match_data = match_data;
 					save_proceed = proceed;
@@ -379,9 +386,8 @@ static int parse_exten(switch_core_session_t *session, switch_caller_profile_t *
 				}
 			}

-			switch_safe_free(field_expanded);
-			if (expression == expression_expanded) expression = NULL;
-			switch_safe_free(expression_expanded);
+			/* field_expanded and expression_expanded stay allocated: after a break out of the loop,
+			 * re and match_data still refer to them. The next condition or done frees them. */
 		} else {
 			if ((xexpression = switch_xml_child(xcond, "expression"))) {
 				expression = switch_str_nil(xexpression->txt);
@@ -449,6 +455,9 @@ static int parse_exten(switch_core_session_t *session, switch_caller_profile_t *
 		}

 		if (save_re) {
+			switch_safe_free(field_expanded);
+			switch_safe_free(expression_expanded);
+
 			re = save_re;
 			match_data = save_match_data;
 			save_re = NULL;
@@ -532,7 +541,7 @@ static int parse_exten(switch_core_session_t *session, switch_caller_profile_t *
 					data = (char *) switch_xml_attr_soft(xaction, "data");
 				}

-				if (field && expression && strchr(expression, '(')) {
+				if (field && expression && match_data && strchr(expression, '(')) {
 					len = (uint32_t) (strlen(data) + strlen(field_data) + 10) * proceed;
 					if (!(substituted = malloc(len))) {
 						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Memory Error!\n");
diff --git a/tests/unit/conf/freeswitch.xml b/tests/unit/conf/freeswitch.xml
index e7c185531c..7997d765cb 100644
--- a/tests/unit/conf/freeswitch.xml
+++ b/tests/unit/conf/freeswitch.xml
@@ -73,5 +73,64 @@
         </condition>
       </extension>
     </context>
+
+    <context name="regex_all_capture">
+      <extension name="regex_all_capture">
+        <condition regex="all">
+          <regex field="${regex_test_unset}" expression="^$"/>
+          <regex field="${regex_test_uri}" expression="^sip:1(\d+)@"/>
+          <action application="set" data="captured=$1"/>
+        </condition>
+      </extension>
+    </context>
+
+    <context name="regex_all_fail_after_capture">
+      <extension name="regex_all_fail_after_capture">
+        <condition regex="all">
+          <regex field="${regex_test_uri}" expression="^sip:1(\d+)@"/>
+          <regex field="${regex_test_uri}" expression="^tel:"/>
+          <action application="set" data="captured=$1"/>
+          <anti-action application="set" data="captured=none"/>
+        </condition>
+      </extension>
+    </context>
+
+    <context name="regex_any_capture">
+      <extension name="regex_any_capture">
+        <condition regex="any">
+          <regex field="${regex_test_uri}" expression="^sip:1(\d+)@"/>
+          <action application="set" data="captured=$1"/>
+        </condition>
+      </extension>
+    </context>
+
+    <context name="regex_xor_capture_then_fail">
+      <extension name="regex_xor_capture_then_fail">
+        <condition regex="xor">
+          <regex field="${regex_test_uri}" expression="^sip:1(\d+)@"/>
+          <regex field="${regex_test_uri}" expression="^tel:(\d+)"/>
+          <action application="set" data="captured=$1"/>
+        </condition>
+      </extension>
+    </context>
+
+    <context name="regex_xor_no_capture_then_fail">
+      <extension name="regex_xor_no_capture_then_fail">
+        <condition regex="xor">
+          <regex field="${regex_test_uri}" expression="^sip:"/>
+          <regex field="${regex_test_uri}" expression="^tel:(\d+)"/>
+          <action application="set" data="captured=$1"/>
+        </condition>
+      </extension>
+    </context>
+
+    <context name="regex_any_expanded_expression_capture">
+      <extension name="regex_any_expanded_expression_capture">
+        <condition regex="any">
+          <regex field="${regex_test_uri}" expression="^sip:${regex_test_prefix}(\d+)@"/>
+          <action application="set" data="captured=$1"/>
+        </condition>
+      </extension>
+    </context>
   </section>
 </document>
diff --git a/tests/unit/switch_ivr_originate.c b/tests/unit/switch_ivr_originate.c
index 5603e18f0e..85c8429a82 100644
--- a/tests/unit/switch_ivr_originate.c
+++ b/tests/unit/switch_ivr_originate.c
@@ -95,6 +95,25 @@ static void loopback_group_confirm_event_handler(switch_event_t *event) // gener
 	}
 }

+/* Runs the XML dialplan in the given context of tests/unit/conf/freeswitch.xml */
+static switch_caller_extension_t *dialplan_regex_hunt(switch_core_session_t *session, const char *context)
+{
+	switch_channel_t *channel = switch_core_session_get_channel(session);
+	switch_caller_profile_t *caller_profile = switch_caller_profile_clone(session, switch_channel_get_caller_profile(channel));
+	switch_dialplan_interface_t *dialplan_interface = switch_loadable_module_get_dialplan_interface("XML");
+	switch_caller_extension_t *extension = NULL;
+
+	if (dialplan_interface) {
+		switch_channel_set_variable(channel, "regex_test_uri", "sip:15551234567@example.com");
+		switch_channel_set_variable(channel, "regex_test_prefix", "1");
+		caller_profile->context = switch_core_session_strdup(session, context);
+		extension = dialplan_interface->hunt_function(session, NULL, caller_profile);
+		UNPROTECT_INTERFACE(dialplan_interface);
+	}
+
+	return extension;
+}
+
 FST_CORE_BEGIN("./conf")
 {
 	FST_SUITE_BEGIN(switch_ivr_originate)
@@ -113,6 +132,145 @@ FST_CORE_BEGIN("./conf")
 		}
 		FST_TEARDOWN_END()

+		/* regex="all" where the last regex has a ${var} field and a capture group: $1 is the captured digits */
+		FST_TEST_BEGIN(dialplan_regex_all_expanded_field_capture)
+		{
+			switch_core_session_t *session = NULL;
+			switch_caller_extension_t *extension = NULL;
+			switch_call_cause_t cause = SWITCH_CAUSE_NONE;
+			switch_status_t status;
+			const char *expected[] = { "set", "captured=5551234567", NULL };
+
+			status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
+			fst_check(status == SWITCH_STATUS_SUCCESS);
+			fst_check(session != NULL);
+
+			if (session) {
+				extension = dialplan_regex_hunt(session, "regex_all_capture");
+				fst_check_extension_apps(expected, extension);
+
+				switch_channel_hangup(switch_core_session_get_channel(session), SWITCH_CAUSE_NORMAL_CLEARING);
+				switch_core_session_rwunlock(session);
+			}
+		}
+		FST_TEST_END()
+
+		/* regex="all" where a ${var} field fails after a capture: the anti-action runs */
+		FST_TEST_BEGIN(dialplan_regex_all_expanded_field_fail_after_capture)
+		{
+			switch_core_session_t *session = NULL;
+			switch_caller_extension_t *extension = NULL;
+			switch_call_cause_t cause = SWITCH_CAUSE_NONE;
+			switch_status_t status;
+			const char *expected[] = { "set", "captured=none", NULL };
+
+			status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
+			fst_check(status == SWITCH_STATUS_SUCCESS);
+			fst_check(session != NULL);
+
+			if (session) {
+				extension = dialplan_regex_hunt(session, "regex_all_fail_after_capture");
+				fst_check_extension_apps(expected, extension);
+
+				switch_channel_hangup(switch_core_session_get_channel(session), SWITCH_CAUSE_NORMAL_CLEARING);
+				switch_core_session_rwunlock(session);
+			}
+		}
+		FST_TEST_END()
+
+		/* regex="any" where the first regex has a ${var} field and a capture group: $1 is the captured digits */
+		FST_TEST_BEGIN(dialplan_regex_any_expanded_field_capture)
+		{
+			switch_core_session_t *session = NULL;
+			switch_caller_extension_t *extension = NULL;
+			switch_call_cause_t cause = SWITCH_CAUSE_NONE;
+			switch_status_t status;
+			const char *expected[] = { "set", "captured=5551234567", NULL };
+
+			status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
+			fst_check(status == SWITCH_STATUS_SUCCESS);
+			fst_check(session != NULL);
+
+			if (session) {
+				extension = dialplan_regex_hunt(session, "regex_any_capture");
+				fst_check_extension_apps(expected, extension);
+
+				switch_channel_hangup(switch_core_session_get_channel(session), SWITCH_CAUSE_NORMAL_CLEARING);
+				switch_core_session_rwunlock(session);
+			}
+		}
+		FST_TEST_END()
+
+		/* regex="any" where the expression has a ${var} and a capture group: $1 is the captured digits */
+		FST_TEST_BEGIN(dialplan_regex_any_expanded_expression_capture)
+		{
+			switch_core_session_t *session = NULL;
+			switch_caller_extension_t *extension = NULL;
+			switch_call_cause_t cause = SWITCH_CAUSE_NONE;
+			switch_status_t status;
+			const char *expected[] = { "set", "captured=5551234567", NULL };
+
+			status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
+			fst_check(status == SWITCH_STATUS_SUCCESS);
+			fst_check(session != NULL);
+
+			if (session) {
+				extension = dialplan_regex_hunt(session, "regex_any_expanded_expression_capture");
+				fst_check_extension_apps(expected, extension);
+
+				switch_channel_hangup(switch_core_session_get_channel(session), SWITCH_CAUSE_NORMAL_CLEARING);
+				switch_core_session_rwunlock(session);
+			}
+		}
+		FST_TEST_END()
+
+		/* regex="xor" where the first regex captures and the second one, also with a group, fails: $1 is the captured digits */
+		FST_TEST_BEGIN(dialplan_regex_xor_capture_then_fail)
+		{
+			switch_core_session_t *session = NULL;
+			switch_caller_extension_t *extension = NULL;
+			switch_call_cause_t cause = SWITCH_CAUSE_NONE;
+			switch_status_t status;
+			const char *expected[] = { "set", "captured=5551234567", NULL };
+
+			status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
+			fst_check(status == SWITCH_STATUS_SUCCESS);
+			fst_check(session != NULL);
+
+			if (session) {
+				extension = dialplan_regex_hunt(session, "regex_xor_capture_then_fail");
+				fst_check_extension_apps(expected, extension);
+
+				switch_channel_hangup(switch_core_session_get_channel(session), SWITCH_CAUSE_NORMAL_CLEARING);
+				switch_core_session_rwunlock(session);
+			}
+		}
+		FST_TEST_END()
+
+		/* regex="xor" where the first regex has no group and matches, and the second one, with a group, fails:
+		 * the action data stays as written */
+		FST_TEST_BEGIN(dialplan_regex_xor_no_capture_then_fail)
+		{
+			switch_core_session_t *session = NULL;
+			switch_caller_extension_t *extension = NULL;
+			switch_call_cause_t cause = SWITCH_CAUSE_NONE;
+			switch_status_t status;
+			const char *expected[] = { "set", "captured=$1", NULL };
+
+			status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL);
+			fst_check(status == SWITCH_STATUS_SUCCESS);
+			fst_check(session != NULL);
+
+			if (session) {
+				extension = dialplan_regex_hunt(session, "regex_xor_no_capture_then_fail");
+				fst_check_extension_apps(expected, extension);
+
+				switch_channel_hangup(switch_core_session_get_channel(session), SWITCH_CAUSE_NORMAL_CLEARING);
+				switch_core_session_rwunlock(session);
+			}
+		}
+		FST_TEST_END()
+
 		FST_TEST_BEGIN(originate_test_external_id)
 		{
 			switch_core_session_t *session = NULL;