From: Alan T. DeKok Date: Wed, 19 Mar 2025 04:31:37 +0000 (+0700) Subject: remove local variables back to front X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0e99af7e40836eabda41e75b2a759345b845f1c4;p=thirdparty%2Ffreeradius-server.git remove local variables back to front and update tests, which now pass --- diff --git a/src/lib/unlang/foreach.c b/src/lib/unlang/foreach.c index 86992856b6a..ce300c40512 100644 --- a/src/lib/unlang/foreach.c +++ b/src/lib/unlang/foreach.c @@ -557,6 +557,8 @@ static unlang_action_t unlang_foreach(rlm_rcode_t *p_result, request_t *request, if (gext->value) { state->vpt = gext->vpt; + fr_assert(fr_pair_find_by_da(&request->local_pairs, NULL, gext->value) == NULL); + /* * Create the local variable and populate its value. */ @@ -568,6 +570,8 @@ static unlang_action_t unlang_foreach(rlm_rcode_t *p_result, request_t *request, fr_assert(state->value != NULL); if (gext->key) { + fr_assert(fr_pair_find_by_da(&request->local_pairs, NULL, gext->key) == NULL); + if (fr_pair_append_by_da(request->local_ctx, &state->key, &request->local_pairs, gext->key) < 0) { REDEBUG("Failed creating %s", gext->key->name); *p_result = RLM_MODULE_FAIL; diff --git a/src/lib/unlang/interpret.c b/src/lib/unlang/interpret.c index 0e5e34d738f..9d15001d6e5 100644 --- a/src/lib/unlang/interpret.c +++ b/src/lib/unlang/interpret.c @@ -219,10 +219,21 @@ typedef struct { static int _local_variables_free(unlang_variable_ref_t *ref) { - fr_pair_list_foreach(&ref->request->local_pairs, vp) { - if (vp->da->dict != ref->dict) break; + fr_pair_t *vp, *prev; + + /* + * Local variables are appended to the end of the list. So we remove them by walking backwards + * from the end of the list. + */ + vp = fr_pair_list_tail(&ref->request->local_pairs); + while (vp) { + prev = fr_pair_list_prev(&ref->request->local_pairs, vp); + if (vp->da->dict != ref->dict) { + break; + } (void) fr_pair_delete(&ref->request->local_pairs, vp); + vp = prev; } return 0; diff --git a/src/tests/keywords/foreach-nested b/src/tests/keywords/foreach-nested index 4fc6df014e8..5f0cb55b7b4 100644 --- a/src/tests/keywords/foreach-nested +++ b/src/tests/keywords/foreach-nested @@ -1,9 +1,9 @@ # PRE: foreach # -foreach Filter-Id { - foreach Calling-Station-Id { +foreach thing1 (Filter-Id[*]) { + foreach thing2 (Calling-Station-Id[*]) { reply += { - Called-Station-Id = "%{Foreach-Variable-0} %{Foreach-Variable-1}" + Called-Station-Id = "%{thing1} %{thing2}" } } }