Description
On Unix the function is potentially unsafe, depending on implementation choices made in the platform provided POSIX gethostname
function. In particular its documentation (man
page) reads:
If the null-terminated hostname is too large to fit, then the name is truncated, and no error is returned (but see NOTES below). POSIX.1-2001 says that if such truncation occurs, then it is unspecified whether the returned buffer includes a terminating null byte.
Although glibc
and Linux will both never return a successful non-nullterminated result, Apples documentation even directly specifies this possibility (edit: but doesn't seem to allow it in practice, see below). The configuration is, however, much more generic than those specific systems.
It would be safer to append a single NULL-byte manually in any case.