From 224546e19e6ac3007c6fd272bdea373ae04d8c3d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20=C4=8Cech?= <pcech@redhat.com>
Date: Tue, 28 Mar 2017 09:11:22 +0200
Subject: [PATCH] IFP: Fix of limit = 0 (unlimited result)

If we set limit to 0 it means that result is unlimited. Internally we
restrict number of result by allocation of result array.
In unlimited case there was a bug and zero array was allocated.
This fix allocates neccessary array when we know real result size.

Resolves:
https://pagure.io/SSSD/sssd/issue/3306
---
 src/responder/ifp/ifp_groups.c | 10 +++++++++-
 src/responder/ifp/ifp_users.c  | 20 ++++++++++++++++++--
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/responder/ifp/ifp_groups.c b/src/responder/ifp/ifp_groups.c
index 94d1e84..166cfe7 100644
--- a/src/responder/ifp/ifp_groups.c
+++ b/src/responder/ifp/ifp_groups.c
@@ -86,7 +86,15 @@ static int ifp_groups_list_copy(struct ifp_list_ctx *list_ctx,
 {
     size_t copy_count, i;
 
-    copy_count = ifp_list_ctx_remaining_capacity(list_ctx, result->count);
+    if (list_ctx->limit == 0) {
+        list_ctx->paths = talloc_zero_array(list_ctx, const char *, result->count);
+        if (list_ctx->paths == NULL) {
+            return ENOMEM;
+        }
+        copy_count = result->count;
+    } else {
+        copy_count = ifp_list_ctx_remaining_capacity(list_ctx, result->count);
+    }
 
     for (i = 0; i < copy_count; i++) {
         list_ctx->paths[list_ctx->path_count + i] = \
diff --git a/src/responder/ifp/ifp_users.c b/src/responder/ifp/ifp_users.c
index cc78300..76c9ac9 100644
--- a/src/responder/ifp/ifp_users.c
+++ b/src/responder/ifp/ifp_users.c
@@ -430,7 +430,15 @@ static int ifp_users_list_copy(struct ifp_list_ctx *list_ctx,
 {
     size_t copy_count, i;
 
-    copy_count = ifp_list_ctx_remaining_capacity(list_ctx, result->count);
+    if (list_ctx->limit == 0) {
+        list_ctx->paths = talloc_zero_array(list_ctx, const char *, result->count);
+        if (list_ctx->paths == NULL) {
+            return ENOMEM;
+        }
+        copy_count = result->count;
+    } else {
+        copy_count = ifp_list_ctx_remaining_capacity(list_ctx, result->count);
+    }
 
     for (i = 0; i < copy_count; i++) {
         list_ctx->paths[list_ctx->path_count + i] = \
@@ -892,7 +900,15 @@ static void ifp_users_list_by_domain_and_name_done(struct tevent_req *req)
         goto done;
     }
 
-    copy_count = ifp_list_ctx_remaining_capacity(list_ctx, result->count);
+    if (list_ctx->limit == 0) {
+        list_ctx->paths = talloc_zero_array(list_ctx, const char *, result->count);
+        if (list_ctx->paths == NULL) {
+            goto done;
+        }
+        copy_count = result->count;
+    } else {
+        copy_count = ifp_list_ctx_remaining_capacity(list_ctx, result->count);
+    }
 
     for (i = 0; i < copy_count; i++) {
         list_ctx->paths[i] = ifp_users_build_path_from_msg(list_ctx->paths,
