*BSD News Article 44710


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!msunews!uchinews!ncar!gatech!news.mathworks.com!uhog.mit.edu!bloom-beacon.mit.edu!boulder!news.coop.net!village.org!not-for-mail
From: imp@village.org (Warner Losh)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Major strcmp bug under BSD 2.0?
Date: 30 May 1995 15:08:53 -0600
Organization: The Village
Lines: 34
Message-ID: <3qg1h5$2gl@rover.village.org>
References: <3qfn52$188j@troy.la.locus.com>
NNTP-Posting-Host: rover.village.org

In article <3qfn52$188j@troy.la.locus.com>, Sassan Behzadi
<sassan@locus.com> wrote:
>Am I imagining things or is there a bug in BSD 2.0's strcmp() function?
>If either of the string parameters is NULL the program core dumps.
>Isn't this a major bug? (Making the OS pretty much useless). Is there 
>a simple fix for this ? 

It is a bug, in *YOUR* program.  Dereferencing a NULL pointer is not
allowed, and strcmp is not required by the standard to do what you
expect in this case.

However, that said, you can do something like:

#define strcmp(a,b) strcmp( (a) ? (a) : "", (b) ? (b) : "");

But watch out for statements with side effects.  Alternatively, you
could also do something like:

#define strcmp(a,b) Strcmp((a),(b))

and then define Strcmp to be something like:

int Strcmp( const char *a, const char *b )
{
	return (strcmp( (a) ? (a) : "", (b) ? (b) : ""));
}

The bottom line: Fix your code.

Warner
-- 
Warner Losh		"VMS Forever"		home: imp@village.org
Cyberspace Development, Inc			work: imp@marketplace.com
Makers of TIA, The Internet Adapter.		http://marketplace.com/