bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

xstrdup (NULL)


From: Colin Watson
Subject: xstrdup (NULL)
Date: Sat, 20 Oct 2007 18:15:13 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

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  <address@hidden>

        * 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                                       address@hidden




reply via email to

[Prev in Thread] Current Thread [Next in Thread]