From: Arran Cudbard-Bell Date: Wed, 20 Mar 2019 14:12:21 +0000 (+0700) Subject: Fix various talloc functions and discovered parenting issues X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=376475640bca5b2f7ff2dad6e07cb7c3b8bfc691;p=thirdparty%2Ffreeradius-server.git Fix various talloc functions and discovered parenting issues --- diff --git a/src/lib/server/cf_util.c b/src/lib/server/cf_util.c index 1000264a7b9..e13dc1053b4 100644 --- a/src/lib/server/cf_util.c +++ b/src/lib/server/cf_util.c @@ -1752,10 +1752,12 @@ int cf_pair_in_table(int32_t *out, FR_NAME_NUMBER const *table, CONF_PAIR *cp) /* * Trim the final ", " */ - MEM(list = talloc_realloc_bstr(list, talloc_array_length(list) - 3)); + MEM(list = talloc_realloc_bstr(NULL, list, talloc_array_length(list) - 3)); cf_log_err(cp, "Invalid value \"%s\". Expected one of %s", cf_pair_value(cp), list); + talloc_free(list); + return -1; } diff --git a/src/lib/server/xlat_eval.c b/src/lib/server/xlat_eval.c index 7549180ad48..7283cb471f7 100644 --- a/src/lib/server/xlat_eval.c +++ b/src/lib/server/xlat_eval.c @@ -155,7 +155,7 @@ static char *xlat_fmt_aprint(TALLOC_CTX *ctx, xlat_exp_t const *node) child_str = xlat_fmt_aprint(pool, child); if (child_str) { - n_out = talloc_buffer_append_buffer(out, child_str); + n_out = talloc_buffer_append_buffer(ctx, out, child_str); if (!n_out) { talloc_free(out); talloc_free(pool); @@ -797,7 +797,9 @@ xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_cursor_t *out, /* * Shrink the buffer */ - if ((node->xlat->buf_len > 0) && (slen > 0)) MEM(str = talloc_realloc_bstr(str, (size_t)slen)); + if ((node->xlat->buf_len > 0) && (slen > 0)) { + MEM(str = talloc_realloc_bstr(ctx, str, (size_t)slen)); + } /* * Fixup talloc lineage and assign the diff --git a/src/lib/server/xlat_tokenize.c b/src/lib/server/xlat_tokenize.c index b47f39eb48c..9447b646641 100644 --- a/src/lib/server/xlat_tokenize.c +++ b/src/lib/server/xlat_tokenize.c @@ -685,7 +685,7 @@ static ssize_t xlat_tokenize_literal(TALLOC_CTX *ctx, xlat_exp_t **head, char co /* * Shrink the buffer to the right size */ - MEM(start = talloc_realloc_bstr(start, node->len)); + MEM(start = talloc_realloc_bstr(ctx, start, node->len)); node->fmt = start; return p - fmt; diff --git a/src/lib/util/dict.c b/src/lib/util/dict.c index 18e3c6e0d87..a52eebc065c 100644 --- a/src/lib/util/dict.c +++ b/src/lib/util/dict.c @@ -1161,7 +1161,7 @@ static fr_dict_attr_t *dict_attr_ref_alloc(fr_dict_t *dict, fr_dict_attr_t const } ref_n = talloc_zero(dict->pool, fr_dict_attr_ref_t); - ref_n->tlv.name = talloc_typed_strdup(ref, name); + ref_n->tlv.name = talloc_typed_strdup(ref_n, name); if (!ref_n->tlv.name) { talloc_free(ref_n); fr_strerror_printf("Out of memory"); diff --git a/src/lib/util/regex.c b/src/lib/util/regex.c index 7da94204b1f..4292b1d6506 100644 --- a/src/lib/util/regex.c +++ b/src/lib/util/regex.c @@ -503,7 +503,7 @@ again: * ...and as pcre2_substitute just succeeded actual_len does not include \0. */ if (actual_len < (buff_len - 1)) { - buff = talloc_realloc_bstr(buff, actual_len); + buff = talloc_realloc_bstr(ctx, buff, actual_len); if (!buff) { fr_strerror_printf("reallocing pcre2_substitute result buffer failed"); return -1; diff --git a/src/lib/util/talloc.c b/src/lib/util/talloc.c index 040179e371f..7c497c1a8df 100644 --- a/src/lib/util/talloc.c +++ b/src/lib/util/talloc.c @@ -227,17 +227,17 @@ TALLOC_CTX *talloc_page_aligned_pool(TALLOC_CTX *ctx, void **start, void **end, * memory chunk type to char, which causes all kinds of issues with * verifying VALUE_PAIRs. * - * @param[in] t The talloc context to hang the result off. - * @param[in] p The string you want to duplicate. + * @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. */ -char *talloc_typed_strdup(void const *t, char const *p) +char *talloc_typed_strdup(TALLOC_CTX *ctx, char const *p) { char *n; - n = talloc_strdup(t, p); + n = talloc_strdup(ctx, p); if (!n) return NULL; talloc_set_type(n, char); @@ -250,19 +250,19 @@ char *talloc_typed_strdup(void const *t, char const *p) * memory chunk type to char, which causes all kinds of issues with * verifying VALUE_PAIRs. * - * @param[in] t The talloc context to hang the result off. - * @param[in] fmt The format string. + * @param[in] ctx The talloc context to hang the result off. + * @param[in] fmt The format string. * @return * - Formatted string. * - NULL on error. */ -char *talloc_typed_asprintf(void const *t, char const *fmt, ...) +char *talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt, ...) { char *n; va_list ap; va_start(ap, fmt); - n = talloc_vasprintf(t, fmt, ap); + n = talloc_vasprintf(ctx, fmt, ap); va_end(ap); if (!n) return NULL; talloc_set_type(n, char); @@ -276,18 +276,18 @@ char *talloc_typed_asprintf(void const *t, char const *fmt, ...) * memory chunk type to char, which causes all kinds of issues with * verifying VALUE_PAIRs. * - * @param[in] t The talloc context to hang the result off. - * @param[in] fmt The format string. - * @param[in] ap varadic arguments. + * @param[in] ctx The talloc context to hang the result off. + * @param[in] fmt The format string. + * @param[in] ap varadic arguments. * @return * - Formatted string. * - NULL on error. */ -char *talloc_typed_vasprintf(void const *t, char const *fmt, va_list ap) +char *talloc_typed_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap) { char *n; - n = talloc_vasprintf(t, fmt, ap); + n = talloc_vasprintf(ctx, fmt, ap); if (!n) return NULL; talloc_set_type(n, char); @@ -297,16 +297,16 @@ char *talloc_typed_vasprintf(void const *t, char const *fmt, va_list ap) /** Binary safe strndup function * - * @param[in] t he talloc context to allocate new buffer in. + * @param[in] ctx he talloc context to allocate new buffer in. * @param[in] in String to dup, may contain embedded '\0'. * @param[in] inlen Number of bytes to dup. * @return duped string. */ -char *talloc_bstrndup(void const *t, char const *in, size_t inlen) +char *talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen) { char *p; - p = talloc_array(t, char, inlen + 1); + p = talloc_array(ctx, char, inlen + 1); if (!p) return NULL; /* @@ -318,6 +318,34 @@ char *talloc_bstrndup(void const *t, char const *in, size_t inlen) return p; } +/** Append a bstr to a bstr + * + * @param[in] ctx to allocated. + * @param[in] to string to append to. + * @param[in] from string to append from. + * @param[in] from_len Length of from. + * @return + * - Realloced buffer containing both to and from. + * - NULL on failure. To will still be valid. + */ +char *talloc_bstr_append(TALLOC_CTX *ctx, char *to, char const *from, size_t from_len) +{ + char *n; + size_t to_len; + + to_len = talloc_array_length(to); + if (to[to_len - 1] == '\0') to_len--; /* Inlen should be length of input string */ + + n = talloc_realloc_size(ctx, to, to_len + from_len + 1); + if (!n) return NULL; + + memcpy(n + to_len, from, from_len); + n[to_len + from_len] = '\0'; + talloc_set_type(n, char); + + return n; +} + /** Trim a bstr (char) buffer * * Reallocs to inlen + 1 and '\0' terminates the string buffer. @@ -329,11 +357,11 @@ char *talloc_bstrndup(void const *t, char const *in, size_t inlen) * - The realloced string on success. in then points to invalid memory. * - NULL on failure. In will still be valid. */ -char *talloc_realloc_bstr(char *in, size_t inlen) +char *talloc_realloc_bstr(TALLOC_CTX *ctx, char *in, size_t inlen) { char *n; - n = talloc_realloc_size(talloc_parent(in), in, inlen + 1); + n = talloc_realloc_size(ctx, in, inlen + 1); if (!n) return NULL; n[inlen] = '\0'; @@ -344,6 +372,7 @@ char *talloc_realloc_bstr(char *in, size_t inlen) /** Concatenate to + from * + * @param[in] ctx to allocate realloced buffer in. * @param[in] to talloc string buffer to append to. * @param[in] from talloc string buffer to append. * @return @@ -354,7 +383,7 @@ char *talloc_realloc_bstr(char *in, size_t inlen) * returns to may point to invalid memory and should * not be used. */ -char *talloc_buffer_append_buffer(char *to, char const *from) +char *talloc_buffer_append_buffer(TALLOC_CTX *ctx, char *to, char const *from) { size_t to_len, from_len, total_len; char *out; @@ -365,7 +394,7 @@ char *talloc_buffer_append_buffer(char *to, char const *from) from_len = talloc_array_length(from); total_len = to_len + (from_len - 1); - out = talloc_realloc(talloc_parent(to), to, char, total_len); + out = talloc_realloc(ctx, to, char, total_len); if (!out) return NULL; memcpy(out + (to_len - 1), from, from_len); @@ -376,6 +405,7 @@ char *talloc_buffer_append_buffer(char *to, char const *from) /** Concatenate to + ... * + * @param[in] ctx to allocate realloced buffer in. * @param[in] to talloc string buffer to append to. * @param[in] argc how many variadic arguments were passed. * @param[in] ... talloc string buffer(s) to append. @@ -389,7 +419,7 @@ char *talloc_buffer_append_buffer(char *to, char const *from) * returns to may point to invalid memory and should * not be used. */ -char *talloc_buffer_append_variadic_buffer(char *to, int argc, ...) +char *talloc_buffer_append_variadic_buffer(TALLOC_CTX *ctx, char *to, int argc, ...) { va_list ap_val, ap_len; int i; @@ -425,7 +455,7 @@ char *talloc_buffer_append_variadic_buffer(char *to, int argc, ...) return to; } - out = talloc_realloc(talloc_parent(to), to, char, total_len + 1); + out = talloc_realloc(ctx, to, char, total_len + 1); if (!out) goto finish; p = out + to_len; diff --git a/src/lib/util/talloc.h b/src/lib/util/talloc.h index 919224adcde..1cf9f3b4571 100644 --- a/src/lib/util/talloc.h +++ b/src/lib/util/talloc.h @@ -42,19 +42,21 @@ int talloc_link_ctx(TALLOC_CTX *parent, TALLOC_CTX *child); TALLOC_CTX *talloc_page_aligned_pool(TALLOC_CTX *ctx, void **start, void **end, size_t size); -char *talloc_typed_strdup(void const *t, char const *p); +char *talloc_typed_strdup(TALLOC_CTX *ctx, char const *p); -char *talloc_typed_asprintf(void const *t, char const *fmt, ...) CC_HINT(format (printf, 2, 3)); +char *talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt, ...) CC_HINT(format (printf, 2, 3)); -char *talloc_typed_vasprintf(void const *t, char const *fmt, va_list ap) CC_HINT(format (printf, 2, 0)) CC_HINT(nonnull (2)); +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_bstrndup(void const *t, char const *in, size_t inlen); +char *talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen); -char *talloc_realloc_bstr(char *in, size_t inlen); +char *talloc_bstr_append(TALLOC_CTX *ctx, char *to, char const *from, size_t from_len); -char *talloc_buffer_append_buffer(char *to, char const *from); +char *talloc_realloc_bstr(TALLOC_CTX *ctx, char *in, size_t inlen); -char *talloc_buffer_append_variadic_buffer(char *to, int argc, ...); +char *talloc_buffer_append_buffer(TALLOC_CTX *ctx, char *to, char const *from); + +char *talloc_buffer_append_variadic_buffer(TALLOC_CTX *ctx, char *to, int argc, ...); int talloc_memcmp_array(uint8_t const *a, uint8_t const *b); diff --git a/src/lib/util/value.c b/src/lib/util/value.c index 94226a7a405..abe6c7b9016 100644 --- a/src/lib/util/value.c +++ b/src/lib/util/value.c @@ -3197,7 +3197,7 @@ int fr_value_box_strdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_ (void) talloc_get_type_abort_const(src, char); len = talloc_array_length(src); - if ((len == 1) || (src[len - 1] != '\0')) { + if ((len == 0) || (src[len - 1] != '\0')) { fr_strerror_printf("Input buffer not \\0 terminated"); return -1; } @@ -4398,7 +4398,7 @@ char *fr_value_box_list_asprint(TALLOC_CTX *ctx, fr_value_box_t const *head, cha str = fr_value_box_asprint(pool, vb, quote); if (!str) continue; - new_aggr = talloc_buffer_append_variadic_buffer(aggr, 2, td, str); + new_aggr = talloc_buffer_append_variadic_buffer(ctx, aggr, 2, td, str); if (unlikely(!new_aggr)) { talloc_free(aggr); talloc_free(pool); diff --git a/src/modules/proto_detail/proto_detail_file.c b/src/modules/proto_detail/proto_detail_file.c index b33601ad670..3fed3207ebb 100644 --- a/src/modules/proto_detail/proto_detail_file.c +++ b/src/modules/proto_detail/proto_detail_file.c @@ -142,7 +142,7 @@ static int mod_open(fr_listen_t *li) } thread->inst = inst; - thread->name = talloc_typed_asprintf(inst, "proto_detail polling for files matching %s", inst->filename); + thread->name = talloc_typed_asprintf(thread, "proto_detail polling for files matching %s", inst->filename); thread->vnode_fd = -1; pthread_mutex_init(&thread->worker_mutex, NULL); diff --git a/src/modules/rlm_cipher/rlm_cipher.c b/src/modules/rlm_cipher/rlm_cipher.c index ab8a499bea1..90ae9935d7c 100644 --- a/src/modules/rlm_cipher/rlm_cipher.c +++ b/src/modules/rlm_cipher/rlm_cipher.c @@ -674,7 +674,7 @@ static xlat_action_t cipher_rsa_decrypt_xlat(TALLOC_CTX *ctx, fr_cursor_t *out, { char *n; - n = talloc_realloc_bstr(plaintext, plaintext_len); + n = talloc_realloc_bstr(ctx, plaintext, plaintext_len); if (unlikely(!n)) { REDEBUG("Failed shrinking plaintext buffer"); talloc_free(plaintext); diff --git a/src/modules/rlm_unbound/rlm_unbound.c b/src/modules/rlm_unbound/rlm_unbound.c index 0a59c221b3d..9c06458b4e6 100644 --- a/src/modules/rlm_unbound/rlm_unbound.c +++ b/src/modules/rlm_unbound/rlm_unbound.c @@ -212,7 +212,7 @@ static int ub_common_fail(REQUEST *request, char const *name, struct ub_result * return 0; } -static ssize_t xlat_a(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen, +static ssize_t xlat_a(TALLOC_CTX *ctx, char **out, size_t outlen, void const *mod_inst, UNUSED void const *xlat_inst, REQUEST *request, char const *fmt) { @@ -227,7 +227,7 @@ static ssize_t xlat_a(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen, /* Used and thus impossible value from heap to designate incomplete */ memcpy(ubres, &mod_inst, sizeof(*ubres)); - fmt2 = talloc_typed_strdup(inst, fmt); + fmt2 = talloc_typed_strdup(ctx, fmt); ub_resolve_async(inst->ub, fmt2, 1, 1, ubres, link_ubres, &async_id); talloc_free(fmt2); @@ -259,7 +259,7 @@ static ssize_t xlat_a(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen, return -1; } -static ssize_t xlat_aaaa(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen, +static ssize_t xlat_aaaa(TALLOC_CTX *ctx, char **out, size_t outlen, void const *mod_inst, UNUSED void const *xlat_inst, REQUEST *request, char const *fmt) { @@ -274,7 +274,7 @@ static ssize_t xlat_aaaa(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen, /* Used and thus impossible value from heap to designate incomplete */ memcpy(ubres, &mod_inst, sizeof(*ubres)); - fmt2 = talloc_typed_strdup(inst, fmt); + fmt2 = talloc_typed_strdup(ctx, fmt); ub_resolve_async(inst->ub, fmt2, 28, 1, ubres, link_ubres, &async_id); talloc_free(fmt2); @@ -304,7 +304,7 @@ error0: return -1; } -static ssize_t xlat_ptr(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen, +static ssize_t xlat_ptr(TALLOC_CTX *ctx, char **out, size_t outlen, void const *mod_inst, UNUSED void const *xlat_inst, REQUEST *request, char const *fmt) { @@ -319,7 +319,7 @@ static ssize_t xlat_ptr(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen, /* Used and thus impossible value from heap to designate incomplete */ memcpy(ubres, &mod_inst, sizeof(*ubres)); - fmt2 = talloc_typed_strdup(inst, fmt); + fmt2 = talloc_typed_strdup(ctx, fmt); ub_resolve_async(inst->ub, fmt2, 12, 1, ubres, link_ubres, &async_id); talloc_free(fmt2);