genhomedircon: sysconf can return -1 without failure

from getpwnam_r(3): "The call sysconf(_SC_GETPW_R_SIZE_MAX) returns
either -1, without changing errno, or an initial suggested size for buf.
(If this size is too small, the call fails with ERANGE, in which case
the caller can retry with a larger buffer.)"

The same can happen for _SC_GETGR_R_SIZE_MAX. 1024 appears to be a good
fallback but may need revisiting in the future.

This triggered an error on musl libc but could happen other places too.

Signed-off-by: Jason Zaman <jason@perfinion.com>
This commit is contained in:
Jason Zaman 2018-05-17 11:21:14 +08:00
parent 178c552e46
commit f1735ebbec

View File

@ -972,9 +972,13 @@ static int add_user(genhomedircon_settings_t * s,
char uid[11];
char gid[11];
errno = 0;
/* Allocate space for the getpwnam_r buffer */
rbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
if (rbuflen <= 0)
if (rbuflen == -1 && errno == 0)
/* sysconf returning -1 with no errno means indeterminate size */
rbuflen = 1024;
else if (rbuflen <= 0)
goto cleanup;
rbuf = malloc(rbuflen);
if (rbuf == NULL)
@ -1057,8 +1061,12 @@ static int get_group_users(genhomedircon_settings_t * s,
struct group grstorage, *group = NULL;
struct passwd *pw = NULL;
errno = 0;
grbuflen = sysconf(_SC_GETGR_R_SIZE_MAX);
if (grbuflen <= 0)
if (grbuflen == -1 && errno == 0)
/* sysconf returning -1 with no errno means indeterminate size */
grbuflen = 1024;
else if (grbuflen <= 0)
goto cleanup;
grbuf = malloc(grbuflen);
if (grbuf == NULL)