Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 23 Apr 2008 14:28:25 +0300
From:      Kostik Belousov <kostikbel@gmail.com>
To:        Simun Mikecin <numisemis@yahoo.com>
Cc:        hackers@freebsd.org, mwm-keyword-freebsdhackers2.e313df@mired.org
Subject:   Re: strdup(NULL) supposed to create SIGSEGV?
Message-ID:  <20080423112825.GX18958@deviant.kiev.zoral.com.ua>
In-Reply-To: <251738.1147.qm@web36602.mail.mud.yahoo.com>
References:  <251738.1147.qm@web36602.mail.mud.yahoo.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--Y+QtaVjtxOtr8cXH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Wed, Apr 23, 2008 at 02:38:54AM -0700, Simun Mikecin wrote:
> >Yes, it's supposed to segfault. Check out what, say, strcpy does if
> >you ask it to copy a NULL pointer. And this is an improvement from the
> >bad old days, when they would happily walk through memory starting at
> >0.....
> >Besides, errno is used to signal errors from system calls. strdup
> >isn't a system call, it's a library function (says so at the top of
> >the man page).
> >Do you have examples of systems where strdup doesn't behave this way?
>=20
> According to Open Group strdup should return NULL and set errno. Look at:
> http://www.opengroup.org/onlinepubs/009695399/functions/strdup.html
>=20
> There is no valid argument for doing segfault instead of above behavior.

No, the Open Group specification says the following in the System
Interfaces -> 2.1 Use and Implementation of Functions:

###
If an argument to a function has an invalid value (such as a value
outside the domain of the function, or a pointer outside the address
space of the program, or a null pointer), the behavior is undefined.
###

Also, see my another answer with the proper incantation from the ANSI C
standard.

--Y+QtaVjtxOtr8cXH
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (FreeBSD)

iEYEARECAAYFAkgPHVkACgkQC3+MBN1Mb4gIQgCbBk7CjAO1FJ13NEf6tjXjSBQt
mzcAoIksACyHmxOG8DRi0GYuzDhVJCFD
=mhIg
-----END PGP SIGNATURE-----

--Y+QtaVjtxOtr8cXH--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080423112825.GX18958>