From 1eebeaa7c363bb5235beccbb31fba7927ac60e49 Mon Sep 17 00:00:00 2001
From: Chris Rienzo <chris.rienzo@grasshopper.com>
Date: Thu, 9 Oct 2014 11:38:53 -0400
Subject: [PATCH] mod_rayo: fix error in SRGS grammar parser...
 <one-of><item>7</item><item>715</item></one-of> will return MATCH_END with
 input of 7 instead of MATCH since 715 is a potential match with further
 input.

---
 src/mod/event_handlers/mod_rayo/srgs.c           | 6 +++++-
 src/mod/event_handlers/mod_rayo/test_srgs/main.c | 9 ++++++---
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/mod/event_handlers/mod_rayo/srgs.c b/src/mod/event_handlers/mod_rayo/srgs.c
index b86704828f..0f387c1e69 100644
--- a/src/mod/event_handlers/mod_rayo/srgs.c
+++ b/src/mod/event_handlers/mod_rayo/srgs.c
@@ -1241,12 +1241,16 @@ static int is_match_end(pcre *compiled_regex, const char *input)
 			search = search_set;
 		}
 		search_input[input_size] = *search++;
-		result = pcre_exec(compiled_regex, NULL, search_input, input_size + 1, 0, 0,
+		result = pcre_exec(compiled_regex, NULL, search_input, input_size + 1, 0, PCRE_PARTIAL,
 			ovector, sizeof(ovector) / sizeof(ovector[0]));
 		if (result > 0) {
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "not match end\n");
 			return 0;
 		}
+		if (result == PCRE_ERROR_PARTIAL) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "partial match possible - not match end\n");
+			return 0;
+		}
 	}
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "is match end\n");
 	return 1;
diff --git a/src/mod/event_handlers/mod_rayo/test_srgs/main.c b/src/mod/event_handlers/mod_rayo/test_srgs/main.c
index 2a102e8939..237e9edad3 100644
--- a/src/mod/event_handlers/mod_rayo/test_srgs/main.c
+++ b/src/mod/event_handlers/mod_rayo/test_srgs/main.c
@@ -11,8 +11,9 @@ static const char *adhearsion_menu_grammar =
 	"    <one-of>\n"
 	"      <item><tag>0</tag>1</item>\n"
 	"      <item><tag>1</tag>5</item>\n"
-	"      <item><tag>2</tag>7</item>\n"
+	"      <item><tag>7</tag>7</item>\n"
 	"      <item><tag>3</tag>9</item>\n"
+	"      <item><tag>4</tag>715</item>\n"
 	"    </one-of>\n"
 	"  </rule>\n"
 	"</grammar>\n";
@@ -43,8 +44,10 @@ static void test_match_adhearsion_menu_grammar(void)
 	ASSERT_STRING_EQUALS("1", interpretation);
 	ASSERT_EQUALS(SMT_NO_MATCH, srgs_grammar_match(grammar, "6", &interpretation));
 	ASSERT_NULL(interpretation);
-	ASSERT_EQUALS(SMT_MATCH_END, srgs_grammar_match(grammar, "7", &interpretation));
-	ASSERT_STRING_EQUALS("2", interpretation);
+	ASSERT_EQUALS(SMT_MATCH, srgs_grammar_match(grammar, "7", &interpretation));
+	ASSERT_STRING_EQUALS("7", interpretation);
+	ASSERT_EQUALS(SMT_MATCH_END, srgs_grammar_match(grammar, "715", &interpretation));
+	ASSERT_STRING_EQUALS("4", interpretation);
 	ASSERT_EQUALS(SMT_NO_MATCH, srgs_grammar_match(grammar, "8", &interpretation));
 	ASSERT_NULL(interpretation);
 	ASSERT_EQUALS(SMT_MATCH_END, srgs_grammar_match(grammar, "9", &interpretation));