Search code examples
linuxmaxstring-length

How big of a buffer should I create for the longest possible user or group name on Linux?


What is the maximum number of characters that a user name or group name may be on Linux?

I need to allocate a buffer and would like to know how much space I need to allocate to guarantee it is large enough for whatever group or user name my application might encounter.


Solution

  • (Putting my comment into an answer now that the question has been reopened)

    POSIX specifies that LOGIN_NAME_MAX must be >= _POSIX_LOGIN_NAME_MAX. _POSIX_LOGIN_NAME_MAX, in turn, is defined to 9. On Linux it seems LOGIN_NAME_MAX is 256.

    For groups, I don't think there is anything similar. Some kind of upper bound can be guesstimated via the getgrnam_r() and getgrgid_r() functions, which take a user supplied buffer for the char* entries in struct group. The maximum needed size for this buffer can be retrieved via sysconf(_SC_GETGR_R_SIZE_MAX) or the macro NSS_BUFLEN_GROUP. On Linux, NSS_BUFLEN_GROUP seems to be 1024.