Merge branch 'dt/xgethostname-nul-termination'
gethostname(2) may not NUL terminate the buffer if hostname does not fit; unfortunately there is no easy way to see if our buffer was too small, but at least this will make sure we will not end up using garbage past the end of the buffer. * dt/xgethostname-nul-termination: xgethostname: handle long hostnames use HOST_NAME_MAX to size buffers for gethostname(2)
This commit is contained in:
13
wrapper.c
13
wrapper.c
@ -655,3 +655,16 @@ void sleep_millisec(int millisec)
|
||||
{
|
||||
poll(NULL, 0, millisec);
|
||||
}
|
||||
|
||||
int xgethostname(char *buf, size_t len)
|
||||
{
|
||||
/*
|
||||
* If the full hostname doesn't fit in buf, POSIX does not
|
||||
* specify whether the buffer will be null-terminated, so to
|
||||
* be safe, do it ourselves.
|
||||
*/
|
||||
int ret = gethostname(buf, len);
|
||||
if (!ret)
|
||||
buf[len - 1] = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user