strdup(NULL) supposed to create SIGSEGV?

Giorgos Keramidas keramida at ceid.upatras.gr
Wed Apr 23 22:20:32 UTC 2008


On Wed, 23 Apr 2008 19:57:47 +0200, Hans Petter Selasky <hselasky at c2i.net> wrote:
> Hi,
>
> I recently had to tell someone that "strncpy" does not always zero
> terminate the destination string. Surprised by what I was telling they
> immediately wanted to change the way the function worked. When a
> function is defined by an ISO standard you are not supposed to change
> the definition. Instead I pointed the person at "strlcpy". Else you
> will have serious trouble when code is ported to a new platform.
>
> http://www.gratisoft.us/todd/papers/strlcpy.html
>
> The name "strdup" is very appealing, but it has already been taken and
> defined. You have to give your variant a different name and convince
> everyone that your function is good and solves a problem so that it
> deserves to be in the C-library.

Right on the spot, Hans :)

You may have to pick a name that doesn't start from "str", though,
because the "str*" function names are reserved for future extensions to
the standard.  ISO/IEC 9899:1999 (E), page 401, §7.26.11 says:

    7.26 Future library directions
    [...]
    7.26.11 String handling <string.h>

    1   Function names that begin with "str", "mem", or "wcs" and a
        lowercase letter may be added to the declarations in the
        <string.h>  header.

You're quite right about 'not replacing' the standard functions in a
lighthearted manner though.  The short-term benefits of making a single
application "easier to write", are dwarfed by the possibilities for
introducing gratuitous incompatibilities for all the _other_ programs
running on the same platform, and any other platforms conforming to the
"real" standard behavior.



More information about the freebsd-hackers mailing list