]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Fix various talloc functions and discovered parenting issues
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 20 Mar 2019 14:12:21 +0000 (21:12 +0700)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 20 Mar 2019 14:13:07 +0000 (21:13 +0700)
src/lib/server/cf_util.c
src/lib/server/xlat_eval.c
src/lib/server/xlat_tokenize.c
src/lib/util/dict.c
src/lib/util/regex.c
src/lib/util/talloc.c
src/lib/util/talloc.h
src/lib/util/value.c
src/modules/proto_detail/proto_detail_file.c
src/modules/rlm_cipher/rlm_cipher.c
src/modules/rlm_unbound/rlm_unbound.c

index 1000264a7b961deae982b53ba2cbcc2ccb485408..e13dc1053b45cc93ca29862226e43814aef9c16e 100644 (file)
@@ -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;
 }
 
index 7549180ad484ac1a179c3ef1735c6a0deb14f3b9..7283cb471f7e5ddba8e713ccbfda0b160e828bc2 100644 (file)
@@ -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
index b47f39eb48cd16ecee6057e15648ec1aaab0b5e6..9447b646641920cf0a40f60330075f2e09c61a68 100644 (file)
@@ -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;
index 18e3c6e0d875dec84e91514b80196be888afadda..a52eebc065c8a03f8436aa713b2f84c902fd2fbb 100644 (file)
@@ -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");
index 7da94204b1fa6835ba6f6135b065bd202ce3a454..4292b1d6506f4e637a77182ed0ebf1970901978d 100644 (file)
@@ -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;
index 040179e371f62ee6019fa151b900b1e9375660fa..7c497c1a8dfaaaee42d20d67f7cdd13cf8b7e0cb 100644 (file)
@@ -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;
index 919224adcde242d2f5d83772e0bad58771540854..1cf9f3b4571f17f6a4a27139495d62129710329c 100644 (file)
@@ -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);
 
index 94226a7a405db7f4c457a1cad278ec26a2f01a20..abe6c7b90163d0bd28b2d0afcd047d4e2f49ff42 100644 (file)
@@ -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);
index b33601ad670b21e0dd8f4474bff3a17cd9b2f841..3fed3207ebbbb9d7e9f8a477ff1f8dc62b21f7cf 100644 (file)
@@ -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);
 
index ab8a499bea134bb98d5fdd292da43c4e124b7fd0..90ae9935d7ce67255dc9fa5198975fb72b7a423e 100644 (file)
@@ -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);
index 0a59c221b3d87755573481d5e24d748a2042776e..9c06458b4e670a4aeb7432fa39a86b3f97b66bf9 100644 (file)
@@ -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);