]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add fr_value_box_bstrassign()
authorAlan T. DeKok <aland@freeradius.org>
Sat, 23 May 2020 12:28:35 +0000 (08:28 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 23 May 2020 12:42:21 +0000 (08:42 -0400)
which avoids talloc_steal().  That function is O(n) in the
number of chunks in the parent's talloc ctx.

src/lib/util/value.c
src/lib/util/value.h

index 0477a3188f4ec94fe5f5baa19e99bed263a477df..d678fb6c393e0a18377ed041027a436761a95d8f 100644 (file)
@@ -3435,6 +3435,45 @@ int fr_value_box_strdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_att
        return 0;
 }
 
+
+/** Assign a nul terminated talloced buffer into a #fr_value_box_t
+ *
+ * The buffer MUST be parented by the input value box.
+ *
+ * The buffer must be \0 terminated, or an error will be returned.
+ *
+ * @param[in] dst      to assign new buffer to.
+ * @param[in] enumv    Aliases for values.
+ * @param[in] src      a talloced nul terminated buffer.
+ * @param[in] tainted  Whether the value came from a trusted source.
+ * @return
+ *     - 0 on success.
+ *     - -1 on failure.
+ */
+int fr_value_box_bstrassign(fr_value_box_t *dst, fr_dict_attr_t const *enumv,
+                           char *src, bool tainted)
+{
+       size_t  len;
+
+       len = talloc_array_length(src);
+       if ((len == 0) || (src[len - 1] != '\0')) {
+               fr_strerror_printf("Input buffer empty or not \\0 terminated");
+               return -1;
+       }
+
+       fr_assert(dst == talloc_parent(src));
+
+       dst->type = FR_TYPE_STRING;
+       dst->tainted = tainted;
+       dst->vb_strvalue = src;
+       dst->datum.length = len - 1;
+       dst->enumv = enumv;
+       dst->next = NULL;
+
+       return 0;
+}
+
+
 /** Steal a nul terminated talloced buffer into a specified ctx, and assign to a #fr_value_box_t
  *
  * Steal a talloced nul terminated buffer, setting fields in the dst value box appropriately.
index cf2a6c59bac7b386ba58669653fbef3565b42f95..0807423f099580b3a8828684d016002a7cbb4c49 100644 (file)
@@ -599,6 +599,8 @@ int         fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t
 void           fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv,
                                              char const *src, size_t len, bool tainted);
 
+int            fr_value_box_bstrassign(fr_value_box_t *dst, fr_dict_attr_t const *enumv,
+                                      char *src, bool tainted);
 int            fr_value_box_bstrsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
                                       char *src, bool tainted);
 int            fr_value_box_bstrsnteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,