getgrgid_r fails with ERANGE if buffer is too small. Retry with a larger buffer.

Signed-off-by: Alexander Kriventsov <akriventsov@nic.ru>
This commit is contained in:
Alexander Kriventsov 2019-06-03 18:11:56 +03:00
parent 3e8a11cb1c
commit b9f80409d7
2 changed files with 26 additions and 1 deletions

View File

@ -206,7 +206,28 @@ static char **get_groupnames(void)
} }
for (i = 0; i < ngroups; i++) { for (i = 0; i < ngroups; i++) {
ret = getgrgid_r(group_ids[i], &grent, buf, bufsize, &grentp); while ((ret = getgrgid_r(group_ids[i], &grent, buf, bufsize, &grentp)) == ERANGE) {
bufsize <<= 1;
if (bufsize > MAX_GRBUF_SIZE) {
usernic_error("Failed to get group members: %u\n",
group_ids[i]);
free(buf);
free(group_ids);
free_groupnames(groupnames);
return NULL;
}
char *new_buf = realloc(buf, bufsize);
if (!new_buf) {
usernic_error("Failed to allocate memory while getting group "
"names: %s\n",
strerror(errno));
free(buf);
free(group_ids);
free_groupnames(groupnames);
return NULL;
}
buf = new_buf;
}
if (!grentp) { if (!grentp) {
if (ret == 0) if (ret == 0)
usernic_error("%s", "Could not find matched group record\n"); usernic_error("%s", "Could not find matched group record\n");

View File

@ -26,6 +26,10 @@
/* Properly support loop devices on 32bit systems. */ /* Properly support loop devices on 32bit systems. */
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
#ifndef MAX_GRBUF_SIZE
#define MAX_GRBUF_SIZE 65536
#endif
#include <errno.h> #include <errno.h>
#include <linux/loop.h> #include <linux/loop.h>
#include <linux/types.h> #include <linux/types.h>