[PATCH 10/14] setpriv: avoid alloca() use xmalloc() instead

Sami Kerola
2014-07-13T20:25:33Z

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 | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/sys-utils/setpriv.c b/sys-utils/setpriv.c
index 65921be..ccfa993 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)
--
2.0.1