Short version: For recent libc releases, and at least on Gentoo,
getgrent() and endgrent() seem to magically set errno to EINVAL (I
think), except when errno's value is actually meaningful.
End of file or error (errno = Success)
errno = Invalid argument
"The setgrent() and endgrent() functions shall not change the setting of
errno if successful."
So I'd advise reporting the bug to the glibc maintainers.