Skip to content

Commit f95146d

Browse files
committed
add more tests and clean up foreach assert
1 parent a78bc1d commit f95146d

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/lib/unlang/foreach.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static unlang_action_t unlang_foreach_xlat_next(rlm_rcode_t *p_result, request_t
256256
*p_result = frame->result;
257257
return UNLANG_ACTION_CALCULATE_RESULT;
258258
}
259-
259+
260260
if (unlang_foreach_xlat_key_update(request, state) < 0) goto next;
261261

262262
fr_value_box_clear_value(&state->value->data);
@@ -418,7 +418,7 @@ static unlang_action_t unlang_foreach_attr_next(rlm_rcode_t *p_result, request_t
418418
}
419419

420420
} else if (fr_type_is_structural(vp->vp_type)) {
421-
fr_assert(state->value->vp_type == vp->vp_type);
421+
if (state->value->vp_type != vp->vp_type) goto next;
422422

423423
fr_pair_list_free(&state->value->vp_group);
424424

@@ -486,6 +486,7 @@ static unlang_action_t unlang_foreach_attr_init(rlm_rcode_t *p_result, request_t
486486
vp = tmpl_dcursor_init(NULL, NULL, &state->cc, &state->cursor, request, state->vpt);
487487
fr_assert(vp != NULL);
488488

489+
next:
489490
/*
490491
* Update the key with the current path. Attribute indexes start at zero.
491492
*/
@@ -500,13 +501,17 @@ static unlang_action_t unlang_foreach_attr_init(rlm_rcode_t *p_result, request_t
500501
}
501502

502503
} else if (fr_type_is_structural(vp->vp_type)) {
503-
if (state->value->vp_type == vp->vp_type) {
504-
if (unlang_foreach_pair_copy(state->value, vp, vp->da) < 0) {
505-
REDEBUG("Failed copying children of %s", state->value->da->name);
506-
goto fail;
507-
}
508-
} else {
509-
REDEBUG("Failed initializing loop variable %s - expected %s type, but got input (%pP)", state->value->da->name, fr_type_to_str(state->value->vp_type), vp);
504+
if (state->value->vp_type != vp->vp_type) {
505+
vp = fr_dcursor_next(&state->cursor);
506+
if (vp) goto next;
507+
508+
*p_result = frame->result;
509+
fr_assert(state->indent == request->log.indent.unlang);
510+
return UNLANG_ACTION_CALCULATE_RESULT;
511+
}
512+
513+
if (unlang_foreach_pair_copy(state->value, vp, vp->da) < 0) {
514+
REDEBUG("Failed copying children of %s", state->value->da->name);
510515
goto fail;
511516
}
512517

src/tests/keywords/foreach-list

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# PRE: foreach
3+
#
4+
string total
5+
6+
#
7+
# Try to delete one of the variables we're looping over. If the user could delete one,
8+
# then the underlying tmpl_dcursor would crash, as it didn't know about the deletion.
9+
#
10+
foreach string value (request.[*]) {
11+
total += value
12+
total += ', '
13+
}
14+
15+
if (total != "bob, hello, Access-Request, ") {
16+
test_fail
17+
}
18+
19+
success

0 commit comments

Comments
 (0)