]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
talloc: Add function to strdup buffers with the type set
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 19 Feb 2023 21:35:33 +0000 (15:35 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 19 Feb 2023 22:55:40 +0000 (16:55 -0600)
src/lib/util/talloc.c
src/lib/util/talloc.h

index e8f2048ef771c6056af613ae6160f25aa7ebd8ef..9254b6d9c64568c0938e827812400cd36e3ae410 100644 (file)
@@ -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
index 275a7999129a60905efcbad5bf3de59aaa5ef1a1..41c36359243ec34982b467d376927418d6c7b41f 100644 (file)
@@ -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));