From: Alan T. DeKok Date: Mon, 30 May 2022 20:49:59 +0000 (-0400) Subject: add function to convert XLAT_FUNC to other type X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4fd7aeec87b756358a763d8ae7f6e2a4e7f481b9;p=thirdparty%2Ffreeradius-server.git add function to convert XLAT_FUNC to other type only in limited situations, and only if we can't leak memory. --- diff --git a/src/lib/unlang/xlat_inst.c b/src/lib/unlang/xlat_inst.c index 730db550822..1b9ab3918ac 100644 --- a/src/lib/unlang/xlat_inst.c +++ b/src/lib/unlang/xlat_inst.c @@ -611,3 +611,37 @@ void xlat_instances_free(void) */ while (xlat_inst_tree && (xi = fr_heap_pop(&xlat_inst_tree))) talloc_free(xi); } + +/** Remove a node from the list of xlat instance data + * + */ +int xlat_inst_remove(xlat_exp_t *node) +{ + int ret; + + fr_assert(node->type == XLAT_FUNC); + fr_assert(!node->call.func->detach); + fr_assert(!node->call.func->thread_detach); + + if (node->call.inst) { + ret = fr_heap_extract(&xlat_inst_tree, node->call.inst); + if (ret < 0) return ret; + + talloc_set_destructor(node->call.inst, NULL); + TALLOC_FREE(node->call.inst); + } + + if (node->call.thread_inst) { + if (!node->call.ephemeral) { + ret = fr_heap_extract(&xlat_thread_inst_tree, node->call.thread_inst); + if (ret < 0) return ret; + } + + talloc_set_destructor(node->call.thread_inst, NULL); + TALLOC_FREE(node->call.inst); + } + + + node->type = XLAT_INVALID; + return 0; +} diff --git a/src/lib/unlang/xlat_priv.h b/src/lib/unlang/xlat_priv.h index c4d5cab4b59..1188a8818ad 100644 --- a/src/lib/unlang/xlat_priv.h +++ b/src/lib/unlang/xlat_priv.h @@ -348,6 +348,11 @@ int xlat_tokenize_function_args(xlat_exp_head_t *head, fr_sbuff_t *in, ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_head_t const *head, xlat_exp_t const *node, fr_sbuff_escape_rules_t const *e_rules); +/* + * xlat_inst.c + */ +int xlat_inst_remove(xlat_exp_t *node); + static inline xlat_exp_t *xlat_exp_head(xlat_exp_head_t const *head) { if (!head) return NULL; diff --git a/src/lib/unlang/xlat_purify.c b/src/lib/unlang/xlat_purify.c index d03234f1720..1f455ee77fb 100644 --- a/src/lib/unlang/xlat_purify.c +++ b/src/lib/unlang/xlat_purify.c @@ -125,11 +125,13 @@ static int xlat_purify_list(xlat_exp_head_t *head, request_t *request) if (!success) return -1; /* - * The function call becomes a GROUP of - * boxes. We just re-use the argument head, which is already of the type we need. + * The function call becomes a GROUP of boxes. We just re-use the argument head, + * which is already of the type we need. */ group = node->call.args; fr_dlist_talloc_free(&group->dlist); + + xlat_inst_remove(node); node->type = XLAT_GROUP; node->group = group;