Skip to content

Commit 49d4a62

Browse files
committed
better handle change xlat type
free tmpls, so that the caller doesn't have to do this. If the tmpl is data, and the new type is XLAT_BOX, then re-initialize the box from the tmpl data, before freeing the tmpl
1 parent 434a5b5 commit 49d4a62

4 files changed

Lines changed: 34 additions & 19 deletions

File tree

src/lib/unlang/xlat_alloc.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,34 @@ void _xlat_exp_set_type(NDEBUG_LOCATION_ARGS xlat_exp_t *node, xlat_type_t type)
7070
TALLOC_FREE(node->group);
7171
break;
7272

73-
case XLAT_FUNC:
7473
case XLAT_FUNC_UNRESOLVED:
75-
if (type != XLAT_FUNC) {
76-
TALLOC_FREE(node->call.args); /* Just switching from unresolved to resolved */
77-
} else goto done;
74+
if (type == XLAT_FUNC) goto done; /* Just switching from unresolved to resolved */
75+
FALL_THROUGH;
76+
77+
case XLAT_FUNC:
78+
TALLOC_FREE(node->call.args);
79+
break;
80+
81+
case XLAT_TMPL:
82+
if (node->vpt && (node->fmt == node->vpt->name)) (void) talloc_steal(node, node->fmt);
83+
84+
/*
85+
* Converting a tmpl to a box. If the tmpl is data, we can then just steal the contents
86+
* of the box.
87+
*/
88+
if (type == XLAT_BOX) {
89+
tmpl_t *vpt = node->vpt;
90+
91+
fr_assert(tmpl_rules_cast(vpt) == FR_TYPE_NULL);
92+
93+
if (tmpl_is_data(vpt)) {
94+
fr_value_box_steal(node, &node->data, tmpl_value(vpt));
95+
} else {
96+
fr_value_box_init_null(&node->data);
97+
}
98+
99+
talloc_free(vpt);
100+
}
78101
break;
79102

80103
default:
@@ -171,6 +194,8 @@ xlat_exp_t *_xlat_exp_alloc(NDEBUG_LOCATION_ARGS TALLOC_CTX *ctx, xlat_type_t ty
171194
inlen + extra);
172195
_xlat_exp_set_type(NDEBUG_LOCATION_VALS node, type);
173196

197+
node->quote = T_BARE_WORD; /* ensure that this is always initialized */
198+
174199
if (type == XLAT_BOX) {
175200
node->flags.constant = node->flags.pure = node->flags.can_purify = true;
176201
}

src/lib/unlang/xlat_expr.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -861,9 +861,6 @@ static bool xlat_node_matches_bool(bool *result, xlat_exp_t *parent, xlat_exp_he
861861

862862
xlat_exp_set_type(parent, XLAT_BOX);
863863
fr_value_box_copy(parent, &parent->data, box);
864-
parent->flags = (xlat_flags_t) { .pure = true, .constant = true, };
865-
866-
talloc_free_children(parent);
867864

868865
return true;
869866
}
@@ -2694,10 +2691,10 @@ static fr_slen_t tokenize_field(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuf
26942691
/*
26952692
* Convert the XLAT_TMPL to XLAT_BOX
26962693
*/
2697-
fr_value_box_steal(node, &node->data, tmpl_value(vpt));
2698-
xlat_exp_set_name_buffer(node, vpt->name);
2699-
talloc_free(vpt);
2694+
node->vpt = NULL;
27002695
xlat_exp_set_type(node, XLAT_BOX);
2696+
xlat_exp_set_name_buffer(node, vpt->name);
2697+
fr_value_box_steal(node, &node->data, tmpl_value(vpt));
27012698
goto done;
27022699

27032700
} else if (tmpl_contains_xlat(node->vpt)) {

src/lib/unlang/xlat_purify.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,9 @@ static int xlat_purify_list_internal(xlat_exp_head_t *head, request_t *request,
146146
* wrapper, which ensures that the entire sub-expression results in one
147147
* output value.
148148
*/
149-
xlat_exp_set_type(node, XLAT_GROUP);
150149
(void) talloc_steal(node, node->vpt->name);
150+
xlat_exp_set_type(node, XLAT_GROUP); /* frees node->vpt */
151151
(void) talloc_steal(node, xlat);
152-
talloc_free(node->vpt);
153152
node->group = xlat;
154153
break;
155154
}

src/lib/unlang/xlat_tokenize.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,7 @@ static int xlat_validate_function_arg(xlat_arg_parser_t const *arg_p, xlat_exp_t
203203
* Raw data can be hoisted to a value-box in this xlat node.
204204
*/
205205
} else if (tmpl_is_data(node->vpt)) {
206-
tmpl_t *vpt = node->vpt;
207-
208-
fr_assert(tmpl_rules_cast(vpt) == FR_TYPE_NULL);
209-
210-
fr_value_box_steal(node, &node->data, tmpl_value(vpt));
211-
talloc_free(vpt);
212-
xlat_exp_set_type(node, XLAT_BOX);
206+
xlat_exp_set_type(node, XLAT_BOX); /* also steals the data */
213207
fr_value_box_mark_safe_for(&node->data, arg_p->safe_for);
214208

215209
} else {

0 commit comments

Comments
 (0)