From: Sami Kerola Date: Sun, 13 Jul 2014 16:58:36 +0000 (+0100) Subject: setpriv: avoid alloca() use xmalloc() instead X-Git-Tag: v2.25~75^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7370501f60f1e7cc386c0f7d5b30eab1824cd613;p=thirdparty%2Futil-linux.git setpriv: avoid alloca() use xmalloc() instead 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 --- diff --git a/sys-utils/setpriv.c b/sys-utils/setpriv.c index 65921be18b..ccfa99333d 100644 --- a/sys-utils/setpriv.c +++ b/sys-utils/setpriv.c @@ -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)