Software Development with Linux

Never assume too much

SUN, 01 MAY 2011

Developing software without making any assumption about the environment or interactions that your piece of code is (will be) interacting with is impossible.  We only have partial information about the different operating systems, third-party libraries, web services, and hardware behaviours that our software either rely on or is expected to cope with.  But be aware that assuming too much about the behaviour of "everything else" is not a good idea, and you should always keep in mind that any assumption may be found to be wrong.

A recent case I have seen of such an assumption which turned out to be wrong is with the gethostname system call.  From gethostname's man page :
The gethostname(char *name, size_tlen) system call returns a null-terminated hostname ... in the array name that has a length of len bytes.
Now, nothing mention what happens to the space left in name after the NUL character.  One can easily assume that nothing will be touched in there.  To my surprise, it seems that at least some versions of HP-UX did not tough so, and even if you memset'ed the name buffer before calling gethostname, you can find junk data after the null-terminated string.

While in itself that's not such a big issue, it shows that even a really simple assumption can be made wrong.

The moral to this story is : if you can make your code work with one less assumption, do it.