summaryrefslogtreecommitdiff
path: root/xmalloc.c
diff options
context:
space:
mode:
authorEugene Syromiatnikov <evgsyr@gmail.com>2017-08-24 17:36:08 +0000
committerEugene Syromyatnikov <evgsyr@gmail.com>2017-08-24 20:06:54 +0200
commit7ba0d89c9f9710bb550201436840079fb71b18be (patch)
tree4d5ecf0035de59decf5cef3e30f547fe75569fd1 /xmalloc.c
parent2b786e0113c105e2434d4e9069797321628b4ab3 (diff)
downloadstrace-7ba0d89c9f9710bb550201436840079fb71b18be.tar.gz
xstrdup, xtrndup: allow NULL argument
Accept NULL argument in xstrdup and xtrndup functions to allow use of "xstrdup(str)" instead of "str ? xstrdup(str) : NULL". * xmalloc.c (xstrdup, xstrndup): Handle NULL argument. * xmalloc.h: Add comment regarding this deviation from the behaviour of the POSIX counterparts of these functions.
Diffstat (limited to 'xmalloc.c')
-rw-r--r--xmalloc.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/xmalloc.c b/xmalloc.c
index 45ff57b15..c2c50f8c6 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -91,6 +91,9 @@ xreallocarray(void *ptr, size_t nmemb, size_t size)
char *
xstrdup(const char *str)
{
+ if (!str)
+ return NULL;
+
char *p = strdup(str);
if (!p)
@@ -104,6 +107,9 @@ xstrndup(const char *str, size_t n)
{
char *p;
+ if (!str)
+ return NULL;
+
#ifdef HAVE_STRNDUP
p = strndup(str, n);
#else