diff --git a/src/lib/server/tmpl_tokenize.c b/src/lib/server/tmpl_tokenize.c index e15247659c04..df05f6d954f8 100644 --- a/src/lib/server/tmpl_tokenize.c +++ b/src/lib/server/tmpl_tokenize.c @@ -3278,14 +3278,7 @@ fr_slen_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, * so that their instance data will be created. */ if (head) { - int rcode; - - if (!t_rules->at_runtime) { - rcode = xlat_bootstrap(head); - } else { - rcode = xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el); - } - if (rcode < 0) { + if (xlat_finalize(head, t_rules) < 0) { fr_strerror_const("Failed to bootstrap xlat"); FR_SBUFF_ERROR_RETURN(&our_in); } diff --git a/src/lib/unlang/xlat.h b/src/lib/unlang/xlat.h index b86be7fe15a3..d9a39a3cf581 100644 --- a/src/lib/unlang/xlat.h +++ b/src/lib/unlang/xlat.h @@ -466,6 +466,8 @@ int xlat_bootstrap(xlat_exp_head_t *root); void xlat_instances_free(void); +int xlat_finalize(xlat_exp_head_t *head, tmpl_rules_t const *t_rules); /* xlat_bootstrap() or xlat_instantiate_ephemeral() */ + /* * xlat_purify.c */ diff --git a/src/lib/unlang/xlat_expr.c b/src/lib/unlang/xlat_expr.c index 49b3a4e2b7ff..0bbdadc6434e 100644 --- a/src/lib/unlang/xlat_expr.c +++ b/src/lib/unlang/xlat_expr.c @@ -2833,7 +2833,6 @@ static const fr_sbuff_term_t operator_terms = FR_SBUFF_TERMS( static fr_slen_t xlat_tokenize_expression_internal(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool cond) { - int rcode; ssize_t slen; fr_sbuff_parse_rules_t *bracket_rules = NULL; fr_sbuff_parse_rules_t *terminal_rules = NULL; @@ -2908,12 +2907,7 @@ static fr_slen_t xlat_tokenize_expression_internal(TALLOC_CTX *ctx, xlat_exp_hea * Add nodes that need to be bootstrapped to * the registry. */ - if (!t_rules || !t_rules->xlat.runtime_el) { - rcode = xlat_bootstrap(head); - } else { - rcode = xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el); - } - if (rcode < 0) { + if (xlat_finalize(head, t_rules) < 0) { talloc_free(head); return -1; } diff --git a/src/lib/unlang/xlat_inst.c b/src/lib/unlang/xlat_inst.c index 9713b0a7a1a9..f00c4ec2bc7b 100644 --- a/src/lib/unlang/xlat_inst.c +++ b/src/lib/unlang/xlat_inst.c @@ -348,9 +348,31 @@ static int _xlat_instantiate_ephemeral_walker(xlat_exp_t *node, void *uctx) return 0; } + +/** Bootstrap static xlats, or instantiate ephemeral ones. + * + * @note - this function should be called when we have a + * #tmpl_rules_t. i.e. instead of calling xlat_bootstrap() or + * xlat_instantiate_ephemeral() + * + * @param[in] head of xlat tree to create instance data for. + * @param[in] t_rules parsing rules with #fr_event_list_t + */ +int xlat_finalize(xlat_exp_head_t *head, tmpl_rules_t const *t_rules) +{ + if (!t_rules || !t_rules->xlat.runtime_el) { + fr_assert(!t_rules->at_runtime); + return xlat_bootstrap(head); + } + + fr_assert(t_rules->at_runtime); + return xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el); +} + /** Create instance data for "ephemeral" xlats * - * @note This must only be used for xlats created at runtime. + * @note This function should only be called from routines which get + * passed a #request_t. * * @param[in] head of xlat tree to create instance data for. * @param[in] el event list used to run any instantiate data @@ -587,9 +609,9 @@ static int _xlat_bootstrap_walker(xlat_exp_t *node, UNUSED void *uctx) /** Create instance data for "permanent" xlats * - * @note This must only be used for xlats created during startup. + * @note This must only be used for xlats which are read from the configuration files. * IF THIS IS CALLED FOR XLATS TOKENIZED AT RUNTIME YOU WILL LEAK LARGE AMOUNTS OF MEMORY. - * USE xlat_instantiate_request() INSTEAD. + * If the caller has a #tmpl_rules_t, it should call xlat_finalize() instead. * * @param[in] head of xlat tree to create instance data for. */ diff --git a/src/lib/unlang/xlat_tokenize.c b/src/lib/unlang/xlat_tokenize.c index c362907f2131..945dcba9e59f 100644 --- a/src/lib/unlang/xlat_tokenize.c +++ b/src/lib/unlang/xlat_tokenize.c @@ -1860,7 +1860,6 @@ fr_slen_t xlat_tokenize_argv(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t fr_slen_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules) { - int rcode; fr_sbuff_t our_in = FR_SBUFF(in); xlat_exp_head_t *head; @@ -1881,12 +1880,7 @@ fr_slen_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in, * Add nodes that need to be bootstrapped to * the registry. */ - if (!t_rules || !t_rules->xlat.runtime_el) { - rcode = xlat_bootstrap(head); - } else { - rcode = xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el); - } - if (rcode < 0) { + if (xlat_finalize(head, t_rules) < 0) { talloc_free(head); return 0; }