Re: strdup(NULL) supposed to create SIGSEGV?



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"



Relevant Pages

  • Re: Help with PAM and ctypes
    ... addr is the memory address as a Python integer. ... the preferred way to create a NULL pointer in ctypes is to call the pointer type without an argument: ... My initial attempt to fix this involved wrapping strdup to allocate a ... copy of a string to send back to PAM. ...
    (comp.lang.python)
  • Re: Crazy stuff
    ... The array is in the automatic-allocation space (we can call this "the ... is exactly what you want - a pointer to the first character of the string. ... Note that strdup is a unixism and not part of the C language. ...
    (comp.lang.c)
  • Re: Im going insane
    ... strdup returns a pointer to a new string which is a duplicate of the string ... Memory for the string is obtained using malloc. ... malloc allocated 300 bytes and returns a pointer to ...
    (comp.unix.programmer)
  • Re: [newbie] question about free() and
    ... pointer to the string. ... If you allocated it with malloc(), ... must not attempt to freea given pointer more than once. ... In any of a number of C books - look for dynamic memory allocation. ...
    (comp.lang.c)
  • Re: Future FASMLIB website
    ... string syntax from C's, many people dislike this a lot. ... wasn't possible to accomodate C format string into asm. ... malloc is supported win32 and dirty-supported (page ... that pointer is in the list of pointers is possible to free, ...
    (alt.lang.asm)