uwrap: Use realloc for setgroups().
authorAndreas Schneider <asn@samba.org>
Fri, 20 Dec 2013 11:54:14 +0000 (12:54 +0100)
committerAndreas Schneider <asn@samba.org>
Fri, 20 Dec 2013 11:54:14 +0000 (12:54 +0100)
src/uid_wrapper.c

index 3c69259bc9f75c33d2e90886573ff0260ead34ca..8e7f761756be4b55698b29c8dd04688afb227f8d 100644 (file)
@@ -859,16 +859,17 @@ static int uwrap_setgroups_thread(size_t size, const gid_t *list)
        int rc = -1;
 
        pthread_mutex_lock(&uwrap_id_mutex);
-       free(id->groups);
-       id->groups = NULL;
-       id->ngroups = 0;
 
-       if (size != 0) {
-               id->groups = malloc(sizeof(gid_t) * size);
-               if (id->groups == NULL) {
+       if (size > 0) {
+               gid_t *tmp;
+
+               tmp = realloc(id->groups, sizeof(gid_t) * size);
+               if (tmp == NULL) {
                        errno = ENOMEM;
                        goto out;
                }
+               id->groups = tmp;
+
                id->ngroups = size;
                memcpy(id->groups, list, size * sizeof(gid_t));
        }
@@ -886,17 +887,18 @@ static int uwrap_setgroups(size_t size, const gid_t *list)
        int rc = -1;
 
        pthread_mutex_lock(&uwrap_id_mutex);
-       for (id = uwrap.ids; id; id = id->next) {
-               free(id->groups);
-               id->groups = NULL;
-               id->ngroups = 0;
 
-               if (size != 0) {
-                       id->groups = malloc(sizeof(gid_t) * size);
-                       if (id->groups == NULL) {
+       if (size > 0) {
+               for (id = uwrap.ids; id; id = id->next) {
+                       gid_t *tmp;
+
+                       tmp = realloc(id->groups, sizeof(gid_t) * size);
+                       if (tmp == NULL) {
                                errno = ENOMEM;
                                goto out;
                        }
+                       id->groups = tmp;
+
                        id->ngroups = size;
                        memcpy(id->groups, list, size * sizeof(gid_t));
                }