Skip to content

Commit

Permalink
make functions take explicit TALLOC_CTX
Browse files Browse the repository at this point in the history
which avoids talloc_parent()
  • Loading branch information
alandekok committed May 21, 2020
1 parent f5c6f00 commit ca635b0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/lib/unlang/xlat_builtin.c
Expand Up @@ -2168,7 +2168,7 @@ static xlat_action_t xlat_func_pack(TALLOC_CTX *ctx, fr_cursor_t *out,
if (vb->vb_length == 0) {
(void) fr_value_box_memcpy(vb, vb, NULL, cast->vb_octets, cast->vb_length, cast->tainted);

} else if (fr_value_box_append_mem(vb, cast->vb_octets, cast->vb_length, cast->tainted) < 0) {
} else if (fr_value_box_append_mem(ctx, vb, cast->vb_octets, cast->vb_length, cast->tainted) < 0) {
goto error;
}

Expand Down
20 changes: 11 additions & 9 deletions src/lib/util/value.c
Expand Up @@ -1678,7 +1678,7 @@ static inline int fr_value_box_cast_to_strvalue(TALLOC_CTX *ctx, fr_value_box_t
}
}

if (fr_value_box_append_bstr(dst, n->vb_strvalue, n->vb_length, n->tainted) < 0) return -1;
if (fr_value_box_append_bstr(ctx, dst, n->vb_strvalue, n->vb_length, n->tainted) < 0) return -1;

if (n != vb) talloc_free(n);
fr_cursor_next(&cursor);
Expand Down Expand Up @@ -1751,7 +1751,7 @@ static inline int fr_value_box_cast_to_octets(TALLOC_CTX *ctx, fr_value_box_t *d
}
}

if (fr_value_box_append_mem(dst, n->vb_octets, n->vb_length, n->tainted) < 0) return -1;
if (fr_value_box_append_mem(ctx, dst, n->vb_octets, n->vb_length, n->tainted) < 0) return -1;

if (n != vb) talloc_free(n);
fr_cursor_next(&cursor);
Expand Down Expand Up @@ -3525,6 +3525,7 @@ int fr_value_box_bstrsnteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t

/** Append a buffer to an existing fr_value_box_t
*
* @param[in] ctx Where to allocate any talloc buffers required.
* @param[in] dst value box to append to.
* @param[in] src octets data to append.
* @param[in] len length of octets data.
Expand All @@ -3533,7 +3534,7 @@ int fr_value_box_bstrsnteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t
* - 0 on success.
* - -1 on failure.
*/
int fr_value_box_append_bstr(fr_value_box_t *dst, char const *src, size_t len, bool tainted)
int fr_value_box_append_bstr(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted)
{
char *ptr, *nptr;
size_t nlen;
Expand All @@ -3547,7 +3548,7 @@ int fr_value_box_append_bstr(fr_value_box_t *dst, char const *src, size_t len, b
return -1;
}

memcpy(&ptr, &dst->datum.ptr, sizeof(ptr)); /* defeat const */
ptr = dst->datum.ptr;
if (!fr_cond_assert(ptr)) return -1;

if (talloc_reference_count(ptr) > 0) {
Expand All @@ -3556,7 +3557,7 @@ int fr_value_box_append_bstr(fr_value_box_t *dst, char const *src, size_t len, b
}

nlen = dst->vb_length + len + 1;
nptr = talloc_realloc(talloc_parent(ptr), ptr, char, dst->vb_length + len + 1);
nptr = talloc_realloc(ctx, ptr, char, dst->vb_length + len + 1);
if (!nptr) {
fr_strerror_printf("%s: Realloc of %s array from %zu to %zu bytes failed",
__FUNCTION__, talloc_get_name(ptr), talloc_array_length(ptr), nlen);
Expand Down Expand Up @@ -3671,6 +3672,7 @@ int fr_value_box_memcpy(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t con

/** Append a buffer to an existing fr_value_box_t
*
* @param[in] ctx Where to allocate any talloc buffers required.
* @param[in] dst value box to append to.
* @param[in] src octets data to append.
* @param[in] len length of octets data.
Expand All @@ -3679,7 +3681,7 @@ int fr_value_box_memcpy(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t con
* - 0 on success.
* - -1 on failure.
*/
int fr_value_box_append_mem(fr_value_box_t *dst, uint8_t const *src, size_t len, bool tainted)
int fr_value_box_append_mem(TALLOC_CTX *ctx, fr_value_box_t *dst, uint8_t const *src, size_t len, bool tainted)
{
uint8_t *ptr, *nptr;
size_t nlen;
Expand All @@ -3702,7 +3704,7 @@ int fr_value_box_append_mem(fr_value_box_t *dst, uint8_t const *src, size_t len,
}

nlen = dst->vb_length + len;
nptr = talloc_realloc(talloc_parent(ptr), ptr, uint8_t, dst->vb_length + len);
nptr = talloc_realloc(ctx, ptr, uint8_t, dst->vb_length + len);
if (!nptr) {
fr_strerror_printf("%s: Realloc of %s array from %zu to %zu bytes failed",
__FUNCTION__, talloc_get_name(ptr), talloc_array_length(ptr), nlen);
Expand Down Expand Up @@ -4810,9 +4812,9 @@ int fr_value_box_list_concat(TALLOC_CTX *ctx,
* Append the next value
*/
if (type == FR_TYPE_STRING) {
if (fr_value_box_append_bstr(out, n->vb_strvalue, n->vb_length, n->tainted) < 0) goto error;
if (fr_value_box_append_bstr(ctx, out, n->vb_strvalue, n->vb_length, n->tainted) < 0) goto error;
} else {
if (fr_value_box_append_mem(out, n->vb_octets, n->vb_length, n->tainted) < 0) goto error;
if (fr_value_box_append_mem(ctx, out, n->vb_octets, n->vb_length, n->tainted) < 0) goto error;
}

if (free_input) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/util/value.h
Expand Up @@ -604,7 +604,7 @@ int fr_value_box_bstrsteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t
int fr_value_box_bstrsnteal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
char **src, size_t inlen, bool tainted);

int fr_value_box_append_bstr(fr_value_box_t *dst, char const *src, size_t len, bool tainted);
int fr_value_box_append_bstr(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted);

void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv,
char const *src, bool tainted);
Expand All @@ -613,7 +613,7 @@ int fr_value_box_strdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr

int fr_value_box_memcpy(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
uint8_t const *src, size_t len, bool tainted);
int fr_value_box_append_mem(fr_value_box_t *dst,
int fr_value_box_append_mem(TALLOC_CTX *ctx, fr_value_box_t *dst,
uint8_t const *src, size_t len, bool tainted);
int fr_value_box_memcpy_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
uint8_t *src, bool tainted);
Expand Down

0 comments on commit ca635b0

Please sign in to comment.