[Asterisk-code-review] res/ari/config.c: Fix user sort compare function. (asterisk[13])

Matt Jordan asteriskteam at digium.com
Tue Sep 29 15:58:13 CDT 2015


Matt Jordan has submitted this change and it was merged.

Change subject: res/ari/config.c: Fix user sort compare function.
......................................................................


res/ari/config.c: Fix user sort compare function.

Made use the ao2 sort compare template function and OBJ_SEARCH_xxx
identifiers.

Change-Id: Ic53005dc5aafa7a36c72300dd89b75fb63c92f4c
---
M res/ari/config.c
1 file changed, 25 insertions(+), 13 deletions(-)

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



diff --git a/res/ari/config.c b/res/ari/config.c
index a6666b6..2d3a80d 100644
--- a/res/ari/config.c
+++ b/res/ari/config.c
@@ -116,19 +116,31 @@
 static int user_sort_cmp(const void *obj_left, const void *obj_right, int flags)
 {
 	const struct ast_ari_conf_user *user_left = obj_left;
+	const struct ast_ari_conf_user *user_right = obj_right;
+	const char *key_right = obj_right;
+	int cmp;
 
-	if (flags & OBJ_PARTIAL_KEY) {
-		const char *key_right = obj_right;
-		return strncasecmp(user_left->username, key_right,
-			strlen(key_right));
-	} else if (flags & OBJ_KEY) {
-		const char *key_right = obj_right;
-		return strcasecmp(user_left->username, key_right);
-	} else {
-		const struct ast_ari_conf_user *user_right = obj_right;
-		const char *key_right = user_right->username;
-		return strcasecmp(user_left->username, key_right);
+	switch (flags & OBJ_SEARCH_MASK) {
+	case OBJ_SEARCH_OBJECT:
+		key_right = user_right->username;
+		/* Fall through */
+	case OBJ_SEARCH_KEY:
+		cmp = strcasecmp(user_left->username, key_right);
+		break;
+	case OBJ_SEARCH_PARTIAL_KEY:
+		/*
+		 * We could also use a partial key struct containing a length
+		 * so strlen() does not get called for every comparison instead.
+		 */
+		cmp = strncasecmp(user_left->username, key_right, strlen(key_right));
+		break;
+	default:
+		/* Sort can only work on something with a full or partial key. */
+		ast_assert(0);
+		cmp = 0;
+		break;
 	}
+	return cmp;
 }
 
 /*! \brief \ref aco_type item_find function */
@@ -138,7 +150,7 @@
 		return NULL;
 	}
 
-	return ao2_find(tmp_container, cat, OBJ_KEY);
+	return ao2_find(tmp_container, cat, OBJ_SEARCH_KEY);
 }
 
 static struct aco_type user_option = {
@@ -234,7 +246,7 @@
 		return NULL;
 	}
 
-	user = ao2_find(conf->users, username, OBJ_KEY);
+	user = ao2_find(conf->users, username, OBJ_SEARCH_KEY);
 	if (!user) {
 		return NULL;
 	}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic53005dc5aafa7a36c72300dd89b75fb63c92f4c
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Matt Jordan <mjordan at digium.com>



More information about the asterisk-code-review mailing list