diff --git a/src/passwd/getgrouplist.c b/src/passwd/getgrouplist.c index 43e51824..44785e10 100644 --- a/src/passwd/getgrouplist.c +++ b/src/passwd/getgrouplist.c @@ -31,12 +31,13 @@ int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups) if (resp[INITGRFOUND]) { nscdbuf = calloc(resp[INITGRNGRPS], sizeof(uint32_t)); if (!nscdbuf) goto cleanup; - if (!fread(nscdbuf, sizeof(*nscdbuf)*resp[INITGRNGRPS], 1, f)) { + size_t ngrps = resp[INITGRNGRPS]; + if (ngrps && !fread(nscdbuf, sizeof(*nscdbuf)*ngrps, 1, f)) { if (!ferror(f)) errno = EIO; goto cleanup; } if (swap) { - for (i = 0; i < resp[INITGRNGRPS]; i++) + for (i = 0; i < ngrps; i++) nscdbuf[i] = bswap_32(nscdbuf[i]); } }