Subject: Re: gethostname and getdomainname
To: Christos Zoulas <>
From: Ignatios Souvatzis <>
List: tech-userlevel
Date: 11/13/1999 23:25:40
On Sat, Nov 13, 1999 at 05:29:51PM +0000, Christos Zoulas wrote:
> In article <>,
> Ignatios Souvatzis <> wrote:
> >
> >Our manual page promises that a truncated name will not be null-terminated.
> >SUSV2 specifies that a truncated name may or may not be null-terminated.
> >
> >I would like to make sysctl null-terminating, for the sake of robustness
> >(The error is reported anyway, and this way, the returned truncated string
> >is ready-to-use if the caller decides to do so).
> Although this is a convenience issue, I'd rather it be non-null-terminated
> for robustness:
> 	- you lose one more byte of information
> 	- if the program does not check the error code, then it'll
> 	  probably get into trouble and the bug will be apparent,
> 	  instead of hidden.

hm... If I like to use the code, with your suggestion, I as a game
author (or game user who wants to fix the code) have to

if (gethostname(buf, sizeof(buf) == -1)
	buf[sizeof(buf)-1] = '\0';

This, or changing the application to provide a bigger buffer, is
easy enough, if the user knows how to use an editor and compiler
and has the sources.

If he doesn't, his game prints to the score file: the score, his
name, @, the start of the hostname, followed by binary gibberish,
filling the terminal until the process gets a SEGV or the file
system fills up.

And this will only happen for users on a machine with a long name, like 
my-hostname-is-longer-than-yours (needing a 33 bytes buffer), while the
stupid game author on jocelyn, also testing on beverly, specified only
32 bytes and never saw the problem.

Would this really help?