gethostname() returns ENOMEM when namelen == 0

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

gethostname() returns ENOMEM when namelen == 0

Kristaps Johnson-2
>Submitter-Id:  net
>Originator:    kristaps
>Organization:
net
>Synopsis:      gethostname() returns ENOMEM when namelen == 0
>Severity:      non-critical
>Priority:      low
>Category:      library
>Class:         sw-bug
>Release:       3.9
>Environment:

         System      : OpenBSD 3.9
         Architecture: OpenBSD.i386
         Machine     : i386
>Description:

The gethostname function returns ENOMEM when the namelen parameter
is set to 0 (even if the name is valid).  The manual page does not
mention this behaviour (it occurs in kern_sysctl.c:sysctl__string):

         if (oldp && *oldlenp < len) {
                 if (trunc == 0 || *oldlenp == 0)
                         return (ENOMEM);
         }

I wasn't sure whether to set this as a doc-bug as other systems
with similar documentation (NetBSD) allow the call to go through.
This behaviour seems to be inherited from sysctl, whose manual
says (for ENOMEM):

         The length pointed to by oldlenp is too short to hold
         the requested value.

>How-To-Repeat:

#include <err.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
         char hn[20];
         if (gethostname(hn, 0) == -1) {
                 err(1, "gethostname");
                 /* NOTREACHED */
         }
         return(0);
}

>Fix: