]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
setpriv: avoid alloca() use xmalloc() instead
authorSami Kerola <kerolasa@iki.fi>
Sun, 13 Jul 2014 16:58:36 +0000 (17:58 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 13 Jul 2014 17:35:38 +0000 (18:35 +0100)
The getgroups() can return up to NGROUPS_MAX supplementary groups, that
is (since kernel 2.6.3) 65536 in total.  The git_t is 4 bytes, so maximum
request is 256 kilobytes.  When a system happen to have memory preasure
alloca() may not be able to allocate enough memory, making debugging
unnecessarily difficult.  IMHO 64 pages is significant enough amount of
memory to be properly error checked at a time of allocation.

Reference: http://www.gnu.org/software/libc/manual/html_node/Disadvantages-of-Alloca.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
sys-utils/setpriv.c

index 65921be18bcdc20b7b6cd4d42a03ca7edcc7f797..ccfa99333d6e8979702d135868f7560037d774c5 100644 (file)
@@ -254,9 +254,10 @@ static void dump_groups(void)
                return;
        }
 
-       groups = alloca(n * sizeof(gid_t));
+       groups = xmalloc(n * sizeof(gid_t));
        n = getgroups(n, groups);
        if (n < 0) {
+               free(groups);
                warn("getgroups failed");
                return;
        }
@@ -273,6 +274,7 @@ static void dump_groups(void)
                }
        }
        printf("\n");
+       free(groups);
 }
 
 static void dump(int dumplevel)