Re: strdup(NULL) supposed to create SIGSEGV?
- From: Mike Meyer <mwm@xxxxxxxxx>
- Date: Wed, 23 Apr 2008 11:39:51 -0400
On Wed, 23 Apr 2008 10:30:39 +0200 Bernard van Gastel <bvgastel@xxxxxxxxxxxxx> wrote:
Op 23 apr 2008, om 08:50 heeft Mike Meyer het volgende geschreven:
On Tue, 22 Apr 2008 22:40:21 -0700
"Garrett Cooper" <yanefbsd@xxxxxxxxx> wrote:
Hi all,
I made an oops in a program, which uncovered "feature" in
strdup(2)
that I wasn't aware of before. So I was wondering, is
strdup(pointer = NULL)
supposed to segfault should this just return NULL and set errno?
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.....
I don't like it this way. I would like:
strdup(NULL) = NULL
strdup(string) = copy of string
strcpy(NULL, NULL) = NULL
strcpy(s1, NULL) = ERROR
strcpy(NULL, s2) = NULL (with s2 unchanged)
strcpy(s1, s2) = normal
But I am not sure of the implications. Maybe in some situation it is
bad... Anyone?
I think someone gave the reason I'm about to: trying to copy a NULL
pointer means I have a bug somewhere earlier in my code that will
eventually produce visibly wrong results - a segfault being such. The
sooner that happens after the bug, the less code I have to search to
find it, the better for me.
So quietly propagating the error is bad in general.
Actually, I'd like to reverse the question: under what conditions
would you be trying to copy a string where not having a string isn't
a sign that something is broken?
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).
But strdup uses malloc, which is a system call (from the strdup
manual: If insufficient memory is available, NULL is returned and
errno is set to ENOMEM.)
As others have pointed out, malloc isn't a system call. However, what
strdup (and malloc) are doing in this case is passing the system error
from what is eventually an internal system call out to their
caller. Basically, errno being set means some system call went wrong,
even if it was made deep in the heart of a library somewhere.
<mike
--
Mike Meyer <mwm@xxxxxxxxx> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
_______________________________________________
freebsd-hackers@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@xxxxxxxxxxx"
- Follow-Ups:
- Re: strdup(NULL) supposed to create SIGSEGV?
- From: Roman Divacky
- Re: strdup(NULL) supposed to create SIGSEGV?
- References:
- strdup(NULL) supposed to create SIGSEGV?
- From: Garrett Cooper
- Re: strdup(NULL) supposed to create SIGSEGV?
- From: Mike Meyer
- Re: strdup(NULL) supposed to create SIGSEGV?
- From: Bernard van Gastel
- strdup(NULL) supposed to create SIGSEGV?
- Prev by Date: Re: devctl (alike?) for devfs
- Next by Date: Re: strdup(NULL) supposed to create SIGSEGV?
- Previous by thread: Re: strdup(NULL) supposed to create SIGSEGV?
- Next by thread: Re: strdup(NULL) supposed to create SIGSEGV?
- Index(es):
Relevant Pages
|