summaryrefslogtreecommitdiff
path: root/source4
diff options
context:
space:
mode:
authorStefan Metzmacher <metze@samba.org>2007-09-18 11:51:41 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 15:06:52 -0500
commit3e58b6fd46518abfd6b6b86623448cae63606bc0 (patch)
treec86200d576403966fded82bb915efdef3f93e244 /source4
parent063daf6ab58dfe5f2d01da0f566a58c0cc77cde0 (diff)
downloadsamba-3e58b6fd46518abfd6b6b86623448cae63606bc0.tar.gz
samba-3e58b6fd46518abfd6b6b86623448cae63606bc0.tar.bz2
samba-3e58b6fd46518abfd6b6b86623448cae63606bc0.zip
r25206: unify logic of talloc_strdup() and talloc_strndup(),
only strlen() vs. strnlen() is the difference now. metze (This used to be commit 92106e6b7e4ed3b6e1aa942b6dc8ae52fdf5805b)
Diffstat (limited to 'source4')
-rw-r--r--source4/lib/talloc/talloc.c42
1 files changed, 20 insertions, 22 deletions
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<n && p[len]; len++) ;
-
- ret = (char *)__talloc(t, len + 1);
- if (!ret) { return NULL; }
- memcpy(ret, p, len);
- ret[len] = 0;
- _talloc_set_name_const(ret, ret);
- return ret;
+ if (unlikely(!p)) return NULL;
+ return __talloc_strlendup(t, p, strnlen(p, n));
}
#ifndef HAVE_VA_COPY