RFR(XS): 8202181: Correctly specify size of hostname buffer in Unix Inet*AddressImpl_getLocalHostName implementations
Langer, Christoph
christoph.langer at sap.com
Tue Apr 24 11:15:00 UTC 2018
Hi Vyom,
> I think, it is intentional to handle case where return "hostname" is to large to
> fit in array. if you see the man page(http://man7.org/linux/man-
> pages/man2/gethostname.2.html) it says that it is unspecified whether
> returned buffer includes a terminating null byte.
>
> current code will put null in case of large "hostname", What do you think ?
yes, I had read the man page and saw this point of the spec. But exactly for this purpose there's this code:
// make sure string is null-terminated
hostname[NI_MAXHOST] = '\0';
If we only hand 'NI_MAXHOST' as size value into gethostname, then the function might only write NI_MAXHOST - 1 characters of the hostname into the buffer.
Best regards
Christoph
More information about the net-dev
mailing list