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

George Joseph asteriskteam at digium.com
Mon Mar 9 10:02:51 CDT 2020


George Joseph has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/13895 )

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(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit



diff --git a/main/enum.c b/main/enum.c
index 344ae40..f4177f9 100644
--- a/main/enum.c
+++ b/main/enum.c
@@ -414,6 +414,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';
@@ -495,13 +496,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. */
@@ -511,7 +522,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/+/13895
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I33943a5b3e7539c6dca3a5079982ee15a08186f0
Gerrit-Change-Number: 13895
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20200309/9e423326/attachment-0001.html>


More information about the asterisk-code-review mailing list