Subject: kern/7836: gethostname fails with small buffer
To: None <>
From: None <>
List: netbsd-bugs
Date: 06/22/1999 20:23:46
>Number:         7836
>Category:       kern
>Synopsis:       gethostname fails with small buffer
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    kern-bug-people (Kernel Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Jun 22 20:05:01 1999
>Originator:     gethostname fails with small buffer
>Release:        NetBSD 1.4C, 9th June 1999
System: NetBSD mona 1.4C NetBSD 1.4C (MONA) #149: Wed Jun 9 11:30:29 EST 1999
simonb@mona:/NetBSD/src/sys/arch/pmax/compile/MONA pmax

	gethostname() fails with ENOMEM if the buffer provided is
	too small, due to gethostname() being implemented with
	sysctl().  Not only is this in conflict with the man page for
	gethostname(), it also doesn't match what the rest of the known
	UNIX world seems to do.

	Compile up an old game and watch the high score table fail
	dismally after killing all the zombies.  Or just try the

	mona:/tmp 132> cat zz.c
		char buf[XX];

		if (gethostname(buf, XX) == 0)
			printf("Hostname: %s\n", buf);
	mona:/tmp 133> cc -DXX=5 zz.c -o zz
	mona:/tmp 134> ./zz 
	Hostname: mona
	mona:/tmp 135> cc -DXX=3 zz.c -o zz
	mona:/tmp 136> ./zz
	gethostname: Cannot allocate memory

	None given.  It'd be real easy to change the manpage to say
	"the buffer must be long enough to hold the hostname.", but I
	don't think this is the right answer.  Maybe sysctl_string()
	in sys/kern/kern_sysctl.c could gain a flag saying it's OK
	to truncate a return string?

	Is gethostname() in any standards?