The hand-rolled implementation of xstrdup I used to use before converting to Gnulib tolerated being passed a NULL string and simply returned NULL. I found this convenient; it saved a number of tedious checks, in much the same way that free doesn't mind being passed a NULL pointer. Would you consider this patch?
(I don't see the need to do the same for xmemdup, since you have to pass it the size of the memory region so it makes sense that that should have to be non-NULL. Though __attribute__ (__nonnull__) might be nice ...) 2007-10-20 Colin Watson <[EMAIL PROTECTED]> * lib/xmalloc.c (xstrdup): Explicitly tolerate a NULL argument, returning NULL in that case. diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 3a12345..e049ff8 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -113,10 +113,12 @@ xmemdup (void const *p, size_t s) return memcpy (xmalloc (s), p, s); } -/* Clone STRING. */ +/* If STRING is NULL, return NULL. Otherwise, clone STRING. */ char * xstrdup (char const *string) { + if (!string) + return NULL; return xmemdup (string, strlen (string) + 1); } Thanks, -- Colin Watson [EMAIL PROTECTED]