From: Pavel Charvat Date: Sun, 7 Nov 2010 14:08:01 +0000 (+0100) Subject: Simplified stdupping of NULL strings. X-Git-Tag: v5.0~132 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=a50ec82cb8c6fe468b8e819c0426f5c8b5268f78;p=libucw.git Simplified stdupping of NULL strings. --- diff --git a/ucw/alloc_str.c b/ucw/alloc_str.c index 5b3d8392..a9543c5b 100644 --- a/ucw/alloc_str.c +++ b/ucw/alloc_str.c @@ -14,6 +14,8 @@ char * xstrdup(const char *s) { + if (!s) + return NULL; uns l = strlen(s) + 1; return memcpy(xmalloc(l), s, l); } diff --git a/ucw/lib.h b/ucw/lib.h index 0190c5a3..79843c0e 100644 --- a/ucw/lib.h +++ b/ucw/lib.h @@ -164,7 +164,7 @@ void *xrealloc(void *, uns); /** Reallocate memory and die() if there is none. void xfree(void *); /** Free memory allocated by xmalloc() or xrealloc(). **/ void *xmalloc_zero(uns) LIKE_MALLOC; /** Allocate memory and fill it by zeroes. **/ -char *xstrdup(const char *) LIKE_MALLOC; /** Make a xmalloc()'ed copy of a string. **/ +char *xstrdup(const char *) LIKE_MALLOC; /** Make a xmalloc()'ed copy of a string. Returns NULL for NULL string. **/ /*** === Trivial timers (timer.c) ***/ diff --git a/ucw/mempool-str.c b/ucw/mempool-str.c index 136f5b4c..cb246630 100644 --- a/ucw/mempool-str.c +++ b/ucw/mempool-str.c @@ -16,6 +16,8 @@ char * mp_strdup(struct mempool *p, const char *s) { + if (!s) + return NULL; uns l = strlen(s) + 1; char *t = mp_alloc_fast_noalign(p, l); memcpy(t, s, l); diff --git a/ucw/mempool.h b/ucw/mempool.h index 7ac7f166..e0d2b43c 100644 --- a/ucw/mempool.h +++ b/ucw/mempool.h @@ -336,7 +336,7 @@ static inline void *mp_realloc_fast(struct mempool *pool, void *ptr, uns size) /** * Save the current state of a memory pool. - * Do not call this function with an opened growing buffer. + * Do not call this function with an opened growing buffer. **/ static inline void mp_save(struct mempool *pool, struct mempool_state *state) { @@ -384,7 +384,7 @@ void mp_pop(struct mempool *pool); * ----------------- ***/ -char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC; /** Makes a copy of a string on a mempool. **/ +char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC; /** Makes a copy of a string on a mempool. Returns NULL for NULL string. **/ void *mp_memdup(struct mempool *, const void *, uns) LIKE_MALLOC; /** Makes a copy of a memory block on a mempool. **/ /** * Concatenates all passed strings. The last parameter must be NULL.