man gethostname on Linux:
The returned name shall be null-terminated, except that if namelen is an insufficient length to hold the host name, then the returned name shall be truncated and it is unspecified whether the returned name is null-terminated.
RETURN VALUE Upon successful completion, 0 shall be returned; otherwise, −1 shall be returned. ERRORS No errors are defined.
And on OpenBSD:
The returned name is always NUL terminated.
ERRORS The following errors may be returned by these calls: [EFAULT] The name parameter gave an invalid address. [ENOMEM] The namelen parameter was zero. [EPERM] The caller tried to set the hostname and was not the superuser.