bug-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fix xgethostname on OSX [patch]


From: Neil Conway
Subject: fix xgethostname on OSX [patch]
Date: Mon, 21 Feb 2005 00:32:24 +1100
User-agent: Mozilla Thunderbird 1.0 (Macintosh/20041206)

The attached one-liner fixes a bug in xgethostname on OSX. It appears that gethostname() on OSX will return ENOMEM via errno if the buffer that was passed is not sufficiently large to hold the hostname (although this isn't documented in the gethostname(3) OSX manpage). The implementation of xgethostname only expects ENAMETOOLONG or EINVAL to be returned in this case, so xgethostname bails out and returns NULL. Since CVS doesn't check the return value of xgethostname() (!), this can result in various badness later on (e.g. invoking strlen() on a NULL pointer, which was how I ran into this -- the hostname on my system happens to be > the initial xgethostname() buffer size).

The attached patch (against cvs 1.12.11) improves xgethostname() to check for ENOMEM as well as ENAMETOOLONG and EINVAL. I grant permission to distribute this patch under the terms of the GNU Public License. Please apply.

BTW, I think there is still room for improvement:

(a) this behavior is not well-standardized. POSIX claims the hostname will be truncated if it is larger than the passed-in buffer length (and it is undefined whether the return value will be NUL terminated), but AFAICS most systems return an error in this case. It seems glibc < 2.1 will return EINVAL, glibc >= 2.1 and FreeBSD will return ENAMETOOLONG, and other systems may well return other errors. It is unnecessary to rely on this behavior, anyway: why not just make the buffer size HOST_NAME_MAX or MAXHOSTNAMELEN (etc.) to begin with? It seems silly to go through this kind of trouble to avoid the allocation of a few hundred bytes of memory.

(b) not checking the return value of xgethostname() is fragile.

-Neil

2005-02-21  Neil Conway  <neilc@samurai.com>

        * lib/xgethostname.c: Check for ENOMEM, which is returned by
        OSX/Darwin if the specified buffer is not large enough for the
        hostname.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]