From 3e58b6fd46518abfd6b6b86623448cae63606bc0 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Tue, 18 Sep 2007 11:51:41 +0000 Subject: r25206: unify logic of talloc_strdup() and talloc_strndup(), only strlen() vs. strnlen() is the difference now. metze (This used to be commit 92106e6b7e4ed3b6e1aa942b6dc8ae52fdf5805b) --- source4/lib/talloc/talloc.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'source4/lib') diff --git a/source4/lib/talloc/talloc.c b/source4/lib/talloc/talloc.c index 7920a66d64..d4c845fa88 100644 --- a/source4/lib/talloc/talloc.c +++ b/source4/lib/talloc/talloc.c @@ -1109,20 +1109,27 @@ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name return newp; } +static inline char *__talloc_strlendup(const void *t, const char *p, size_t len) +{ + char *ret; + + ret = (char *)__talloc(t, len + 1); + if (unlikely(!ret)) return NULL; + + memcpy(ret, p, len); + ret[len] = 0; + + _talloc_set_name_const(ret, ret); + return ret; +} + /* - strdup with a talloc + strdup with a talloc */ char *talloc_strdup(const void *t, const char *p) { - char *ret; - if (!p) { - return NULL; - } - ret = (char *)talloc_memdup(t, p, strlen(p) + 1); - if (likely(ret)) { - _talloc_set_name_const(ret, ret); - } - return ret; + if (unlikely(!p)) return NULL; + return __talloc_strlendup(t, p, strlen(p)); } /* @@ -1152,21 +1159,12 @@ char *talloc_append_string(const void *t, char *orig, const char *append) } /* - strndup with a talloc + strndup with a talloc */ char *talloc_strndup(const void *t, const char *p, size_t n) { - size_t len; - char *ret; - - for (len=0; len