strdup(NULL) supposed to create SIGSEGV?

Kostik Belousov kostikbel at gmail.com
Wed Apr 23 11:28:32 UTC 2008


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?
> 
> According to Open Group strdup should return NULL and set errno. Look at:
> http://www.opengroup.org/onlinepubs/009695399/functions/strdup.html
> 
> 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 195 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20080423/3ccc1607/attachment.pgp


More information about the freebsd-hackers mailing list