From: Arran Cudbard-Bell Date: Sun, 19 Feb 2023 21:35:33 +0000 (-0600) Subject: talloc: Add function to strdup buffers with the type set X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34486915d2f6554673788f2cf88befb35c31b9a7;p=thirdparty%2Ffreeradius-server.git talloc: Add function to strdup buffers with the type set --- diff --git a/src/lib/util/talloc.c b/src/lib/util/talloc.c index e8f2048ef77..9254b6d9c64 100644 --- a/src/lib/util/talloc.c +++ b/src/lib/util/talloc.c @@ -341,6 +341,30 @@ char *talloc_typed_strdup(TALLOC_CTX *ctx, char const *p) return n; } +/** Call talloc_strndup, setting the type on the new chunk correctly + * + * This function is similar to talloc_typed_strdup but gets the chunk + * length using talloc functions. + * + * @param[in] ctx The talloc context to hang the result off. + * @param[in] p The string you want to duplicate. + * @return + * - Duplicated string. + * - NULL on error. + * + * @hidecallergraph + */ +char *talloc_typed_strdup_buffer(TALLOC_CTX *ctx, char const *p) +{ + char *n; + + n = talloc_strndup(ctx, p, talloc_array_length(p) - 1); + if (unlikely(!n)) return NULL; + talloc_set_type(n, char); + + return n; +} + /** Call talloc vasprintf, setting the type on the new chunk correctly * * For some bizarre reason the talloc string functions don't set the diff --git a/src/lib/util/talloc.h b/src/lib/util/talloc.h index 275a7999129..41c36359243 100644 --- a/src/lib/util/talloc.h +++ b/src/lib/util/talloc.h @@ -172,6 +172,8 @@ static inline TALLOC_CTX *_talloc_zero_pooled_object(const void *ctx, /** @hidecallergraph */ char *talloc_typed_strdup(TALLOC_CTX *ctx, char const *p); +char *talloc_typed_strdup_buffer(TALLOC_CTX *ctx, char const *p); + char *talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt, ...) CC_HINT(format (printf, 2, 3)); char *talloc_typed_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap) CC_HINT(format (printf, 2, 0)) CC_HINT(nonnull (2));