[Asterisk-code-review] enum.c: Add support for regular expression flag in NAPTR record (asterisk[13])

Sean Bright asteriskteam at digium.com
Fri Mar 6 15:06:05 CST 2020


Sean Bright has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/13901 )


Change subject: enum.c: Add support for regular expression flag in NAPTR record
......................................................................

enum.c: Add support for regular expression flag in NAPTR record

A regular expression in a NAPTR response record can have a trailing
'i' flag to indicate that the expression should be evaluated in a
case-insensitive way. We were not checking for that flag which caused
the record parsing to fail on otherwise valid input.

Although this change will initially go into Asterisk 13, 16, and 17,
it is my intention to replace the majority of this code in 16 and up -
including this fix - by changing enum.c to consume the new DNS API
which duplicates most of this logic already. Asterisk 13 doesn't have
the DNS API, so this fix will be as good as it gets.

ASTERISK-26711 #close
Reported by: Vitold

Change-Id: I33943a5b3e7539c6dca3a5079982ee15a08186f0
---
M main/enum.c
1 file changed, 13 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/01/13901/1

diff --git a/main/enum.c b/main/enum.c
index e003f07..17bba83 100644
--- a/main/enum.c
+++ b/main/enum.c
@@ -415,6 +415,7 @@
 	int size, matchindex; /* size is the size of the backreference sub. */
 	size_t d_len = sizeof(tempdst) - 1;
 	regex_t preg;
+	int re_flags = REG_EXTENDED | REG_NEWLINE;
 	regmatch_t pmatch[max_bt];
 
 	tech_return[0] = '\0';
@@ -496,13 +497,23 @@
 	 * and uses that character to find the index of the second delimiter */
 	delim = regexp[0];
 	delim2 = strchr(regexp + 1, delim);
-	if ((delim2 == NULL) || (regexp[regexp_len - 1] != delim)) {  /* is the second delimiter found, and is the end of the regexp a delimiter */
+	if ((delim2 == NULL)
+		|| ((regexp[regexp_len - 1] != 'i' || regexp[regexp_len - 2] != delim)
+			&& regexp[regexp_len - 1] != delim)) {
 		ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
 		return -1;
 	} else if (strchr((delim2 + 1), delim) == NULL) { /* if the second delimiter is found, make sure there is a third instance.  this could be the end one instead of the middle */
 		ast_log(LOG_WARNING, "Regex delimiter error (on \"%s\").\n", regexp);
 		return -1;
 	}
+
+	/* Make the regex case-insensitive if the 'i' flag is present. This assumes you
+	 * aren't using 'i' as a delimiter which, altough dubious, does not appear to be
+	 * explicitly non-compliant */
+	if (regexp[regexp_len - 1] == 'i') {
+		re_flags |= REG_ICASE;
+	}
+
 	pattern = regexp + 1;   /* pattern is the regex without the begining and ending delimiter */
 	*delim2 = 0;    /* zero out the middle delimiter */
 	subst   = delim2 + 1; /* dst substring is everything after the second delimiter. */
@@ -512,7 +523,7 @@
  * now do the regex wizardry.
  */
 
-	if (regcomp(&preg, pattern, REG_EXTENDED | REG_NEWLINE)) {
+	if (regcomp(&preg, pattern, re_flags)) {
 		ast_log(LOG_WARNING, "NAPTR Regex compilation error (regex = \"%s\").\n", regexp);
 		return -1;
 	}

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/13901
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Change-Id: I33943a5b3e7539c6dca3a5079982ee15a08186f0
Gerrit-Change-Number: 13901
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200306/e7454a6c/attachment.html>


More information about the asterisk-code-review mailing list