From: Arran Cudbard-Bell Date: Thu, 7 May 2020 19:04:19 +0000 (-0500) Subject: Add talloc bstrdup X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0f485cafc18da31aa894642e8157ae14ffd714fb;p=thirdparty%2Ffreeradius-server.git Add talloc bstrdup --- diff --git a/src/lib/util/talloc.c b/src/lib/util/talloc.c index d72e7ed5639..b7b8c4bfc09 100644 --- a/src/lib/util/talloc.c +++ b/src/lib/util/talloc.c @@ -311,6 +311,30 @@ char *talloc_typed_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap) return n; } +/** Binary safe strdup function + * + * @param[in] ctx he talloc context to allocate new buffer in. + * @param[in] in String to dup, may contain embedded '\0'. + * @return duped string. + */ +char *talloc_bstrdup(TALLOC_CTX *ctx, char const *in) +{ + char *p; + size_t len = talloc_array_length(in); + + if (len == 0) len = 1; + + p = talloc_array(ctx, char, len); + if (!p) return NULL; + + /* + * C99 (7.21.1/2) - Length zero results in noop + */ + memcpy(p, in, len - 1); + p[len] = '\0'; + + return p; +} /** Binary safe strndup function * diff --git a/src/lib/util/talloc.h b/src/lib/util/talloc.h index 779f497b2e0..4946a4723bd 100644 --- a/src/lib/util/talloc.h +++ b/src/lib/util/talloc.h @@ -101,6 +101,8 @@ char *talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt, ...) CC_HINT(form char *talloc_typed_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap) CC_HINT(format (printf, 2, 0)) CC_HINT(nonnull (2)); +char *talloc_bstrdup(TALLOC_CTX *ctx, char const *in); + char *talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen); char *talloc_bstr_append(TALLOC_CTX *ctx, char *to, char const *from, size_t from_len);