[Asterisk-code-review] pbx realtime: Prevent premature extension matching (asterisk[14])

Anonymous Coward asteriskteam at digium.com
Wed Feb 22 12:08:53 CST 2017


Anonymous Coward #1000019 has submitted this change and it was merged. ( https://gerrit.asterisk.org/5060 )

Change subject: pbx_realtime: Prevent premature extension matching
......................................................................


pbx_realtime: Prevent premature extension matching

The patterns provided by pbx_realtime were checked in the order in
which they were returned from the realtime backend. If there was
overlap between multiple patterns, the first one to correctly match was
chosen even though it may not have been the best match.

We now sort the patterns descending by their length and compare in that
order. There may be cases where this still results in a sub-optimal
match, but this patch should improve the overall behavior.

ASTERISK-18271 #close
Reported by: Charlie Smurthwaite

Change-Id: I56d9ac15810eb1775966b669c3028e32cc7bd809
---
M pbx/pbx_realtime.c
1 file changed, 10 insertions(+), 0 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  Anonymous Coward #1000019: Verified
  Joshua Colp: Looks good to me, approved



diff --git a/pbx/pbx_realtime.c b/pbx/pbx_realtime.c
index b0e294b..9c807f8 100644
--- a/pbx/pbx_realtime.c
+++ b/pbx/pbx_realtime.c
@@ -143,6 +143,13 @@
 	return NULL;
 }
 
+static int extension_length_comparator(struct ast_category *p, struct ast_category *q)
+{
+	const char *extenp = S_OR(ast_variable_find(p, "exten"), "");
+	const char *extenq = S_OR(ast_variable_find(q, "exten"), "");
+
+	return strlen(extenp) - strlen(extenq);
+}
 
 /* Realtime switch looks up extensions in the supplied realtime table.
 
@@ -193,6 +200,9 @@
 		if (cfg) {
 			char *cat = NULL;
 
+			/* Sort so that longer patterns are checked first */
+			ast_config_sort_categories(cfg, 1, extension_length_comparator);
+
 			while ((cat = ast_category_browse(cfg, cat))) {
 				const char *realtime_exten = ast_variable_retrieve(cfg, cat, "exten");
 

-- 
To view, visit https://gerrit.asterisk.org/5060
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I56d9ac15810eb1775966b669c3028e32cc7bd809
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 14
Gerrit-Owner: Sean Bright <sean.bright at gmail.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>



More information about the asterisk-code-review mailing list