Re: strdup(NULL) supposed to create SIGSEGV?

看板FB_hackers作者時間17年前 (2008/04/23 19:29), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串11/25 (看更多)
--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--
文章代碼(AID): #183nsa00 (FB_hackers)
討論串 (同標題文章)
文章代碼(AID): #183nsa00 (FB_hackers)