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 <[email protected]>
diff --git a/libsemanage/src/genhomedircon.c b/libsemanage/src/genhomedircon.c
index d09d82f..3e61b51 100644
--- a/libsemanage/src/genhomedircon.c
+++ b/libsemanage/src/genhomedircon.c
@@ -972,9 +972,13 @@
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 @@
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)