From 9c03180cadcc8bde2da5037156034ba4591a6c15 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Fri, 2 Aug 2013 23:07:53 +0200
Subject: [PATCH 1/4] resolv_sort_srv_reply: remove unnecessary mem_ctx

---
 src/providers/fail_over_srv.c |  2 +-
 src/resolv/async_resolv.c     | 18 +++++++++++-------
 src/resolv/async_resolv.h     |  2 +-
 src/tests/resolv-tests.c      |  4 ++--
 4 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/providers/fail_over_srv.c b/src/providers/fail_over_srv.c
index 78cd6cc4593fbdec89062d1f109ccbcb38d7314a..7fca2668977a2231c7669f6b4b8302876712e95d 100644
--- a/src/providers/fail_over_srv.c
+++ b/src/providers/fail_over_srv.c
@@ -98,7 +98,7 @@ static void fo_discover_srv_done(struct tevent_req *subreq)
     DEBUG(SSSDBG_TRACE_FUNC, ("Got answer. Processing...\n"));
 
     /* sort and store the answer */
-    ret = resolv_sort_srv_reply(state, &reply_list);
+    ret = resolv_sort_srv_reply(&reply_list);
     if (ret != EOK) {
         DEBUG(SSSDBG_CRIT_FAILURE, ("Could not sort the answers from DNS "
                                     "[%d]: %s\n", ret, strerror(ret)));
diff --git a/src/resolv/async_resolv.c b/src/resolv/async_resolv.c
index 0a2c5189f8ebf2f10e326e133718714e06de1b8a..dda288575f62fde9f3553402bf70382d27c535a3 100644
--- a/src/resolv/async_resolv.c
+++ b/src/resolv/async_resolv.c
@@ -2048,8 +2048,7 @@ static struct ares_srv_reply *reply_priority_sort(struct ares_srv_reply *list)
     return list;
 }
 
-static int reply_weight_rearrange(TALLOC_CTX *mem_ctx,
-                                  int len,
+static int reply_weight_rearrange(int len,
                                   struct ares_srv_reply **start,
                                   struct ares_srv_reply **end)
 {
@@ -2059,12 +2058,13 @@ static int reply_weight_rearrange(TALLOC_CTX *mem_ctx,
     struct ares_srv_reply *r, *prev, *tmp;
     struct ares_srv_reply *new_start = NULL;
     struct ares_srv_reply *new_end = NULL;
+    int ret;
 
     if (len <= 1) {
         return EOK;
     }
 
-    totals = talloc_array(mem_ctx, int, len);
+    totals = talloc_array(NULL, int, len);
     if (!totals) {
         return ENOMEM;
     }
@@ -2122,7 +2122,8 @@ static int reply_weight_rearrange(TALLOC_CTX *mem_ctx,
 
         if (r == NULL || totals[i] == -1) {
             DEBUG(1, ("Bug: did not select any server!\n"));
-            return EIO;
+            ret = EIO;
+            goto done;
         }
 
         /* remove r from the old list */
@@ -2146,12 +2147,15 @@ static int reply_weight_rearrange(TALLOC_CTX *mem_ctx,
     /* return the rearranged list */
     *start = new_start;
     *end = new_end;
+    ret = EOK;
+
+done:
     talloc_free(totals);
-    return EOK;
+    return ret;
 }
 
 int
-resolv_sort_srv_reply(TALLOC_CTX *mem_ctx, struct ares_srv_reply **reply)
+resolv_sort_srv_reply(struct ares_srv_reply **reply)
 {
     int ret;
     struct ares_srv_reply *pri_start, *pri_end, *next, *prev_end;
@@ -2184,7 +2188,7 @@ resolv_sort_srv_reply(TALLOC_CTX *mem_ctx, struct ares_srv_reply **reply)
         /* rearrange each priority level according to the weight field */
         next = pri_end->next;
         pri_end->next = NULL;
-        ret = reply_weight_rearrange(mem_ctx, len, &pri_start, &pri_end);
+        ret = reply_weight_rearrange(len, &pri_start, &pri_end);
         if (ret) {
             DEBUG(1, ("Error rearranging priority level [%d]: %s\n",
                       ret, strerror(ret)));
diff --git a/src/resolv/async_resolv.h b/src/resolv/async_resolv.h
index 2895753b4b43573cc935c112eb5518edbedf00b0..919ba370b87304735bff279d1502386d2ac28735 100644
--- a/src/resolv/async_resolv.h
+++ b/src/resolv/async_resolv.h
@@ -148,7 +148,7 @@ int resolv_getsrv_recv(TALLOC_CTX *mem_ctx,
 
 /* This is an implementation of section "Usage rules" of RFC 2782 */
 int
-resolv_sort_srv_reply(TALLOC_CTX *mem_ctx, struct ares_srv_reply **reply);
+resolv_sort_srv_reply(struct ares_srv_reply **reply);
 
 /** Get TXT record **/
 struct tevent_req *resolv_gettxt_send(TALLOC_CTX *mem_ctx,
diff --git a/src/tests/resolv-tests.c b/src/tests/resolv-tests.c
index a8e4c4120cee286890dfdd67f3176fdf0463c162..8fde675f4040e47d1387858d1512c310a85369f6 100644
--- a/src/tests/resolv-tests.c
+++ b/src/tests/resolv-tests.c
@@ -722,7 +722,7 @@ START_TEST(test_resolv_sort_srv_reply)
     }
 
     /* do the sort */
-    ret = resolv_sort_srv_reply(test_ctx, &replies);
+    ret = resolv_sort_srv_reply(&replies);
     fail_if(ret != EOK);
 
     /* check if the list is sorted */
@@ -757,7 +757,7 @@ START_TEST(test_resolv_sort_srv_reply)
     }
 
     /* do the sort */
-    ret = resolv_sort_srv_reply(test_ctx, &replies);
+    ret = resolv_sort_srv_reply(&replies);
     fail_if(ret != EOK);
 
     /* clean up */
-- 
1.7.11.7

