r2451: Fix from Henrik Nordstrom <hno@squid-cache.org> to allow
authorJeremy Allison <jra@samba.org>
Mon, 20 Sep 2004 20:18:19 +0000 (20:18 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 15:52:43 +0000 (10:52 -0500)
winbindd to return the correct number of groups when the
groups array must be enlarged.
Jeremy.
(This used to be commit bcc769de4d60205209633887f2fb2f0ab6088cae)

source3/nsswitch/winbind_nss_linux.c

index ae2bcc7ade98c60e3b6dc4171b2b0588229e9873..a6d0bfe4e86e8761089da331671fcc85a88e431e 100644 (file)
@@ -833,25 +833,38 @@ _nss_winbind_initgroups_dyn(char *user, gid_t group, long int *start,
 
                        /* Skip primary group */
 
-                       if (gid_list[i] == group) continue;
-
-                       /* Add to buffer */
+                       if (gid_list[i] == group) {
+                               continue;
+                       }
 
-                       if (*start == *size && limit <= 0) {
-                               (*groups) = realloc(
-                                       (*groups), (2 * (*size) + 1) * sizeof(**groups));
-                               if (! *groups) goto done;
-                               *size = 2 * (*size) + 1;
+                       /* Filled buffer ? If so, resize. */
+
+                       if (*start == *size) {
+                               long int newsize;
+                               gid_t *newgroups;
+
+                               newsize = 2 * (*size);
+                               if (limit > 0) {
+                                       if (*size == limit) {
+                                               goto done;
+                                       }
+                                       newsize = MIN(newsize, limit);
+                               }
+
+                               newgroups = realloc((*groups), newsize * sizeof(**groups));
+                               if (!newgroups) {
+                                       *errnop = ENOMEM;
+                                       ret = NSS_STATUS_NOTFOUND;
+                                       goto done;
+                               }
+                               *groups = newgroups;
+                               *size = newsize;
                        }
 
-                       if (*start == *size) goto done;
+                       /* Add to buffer */
 
                        (*groups)[*start] = gid_list[i];
                        *start += 1;
-
-                       /* Filled buffer? */
-
-                       if (*start == limit) goto done;
                }
        }