From: Alan T. DeKok Date: Fri, 14 Jul 2023 20:24:35 +0000 (-0400) Subject: update fr_pair_list_copy_by_ancestor() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=33cd88bc266025380ad916e60b648afb2c3252e3;p=thirdparty%2Ffreeradius-server.git update fr_pair_list_copy_by_ancestor() if we see a TLV, we just copy that. The function can then work both with nested and non-nested attributes. we don't need to pass "count", as the only caller always says "copy all of them" --- diff --git a/src/lib/util/pair.c b/src/lib/util/pair.c index 27867ebab40..2707f95dbbd 100644 --- a/src/lib/util/pair.c +++ b/src/lib/util/pair.c @@ -2236,49 +2236,45 @@ int fr_pair_list_copy_by_da(TALLOC_CTX *ctx, fr_pair_list_t *to, * @param[in] to where to copy attributes to. * @param[in] from whence to copy #fr_pair_t (s). * @param[in] parent_da to match. - * @param[in] count How many instances to copy. - * Use 0 for all attributes. * @return - * - >0 the number of attributes copied. + * - >0 one or more attributes were copied * - 0 if no attributes copied. * - -1 on error. */ int fr_pair_list_copy_by_ancestor(TALLOC_CTX *ctx, fr_pair_list_t *to, - fr_pair_list_t const *from, fr_dict_attr_t const *parent_da, unsigned int count) + fr_pair_list_t const *from, fr_dict_attr_t const *parent_da) { - fr_pair_t *vp, *new_vp, *tlv; - unsigned int cnt = 0; - - if (count == 0) count = UINT_MAX; + fr_pair_t *tlv; + bool found = false; /* * Allow for nested attributes. */ tlv = fr_pair_find_by_da(from, NULL, parent_da); if (tlv) { - switch (parent_da->type) { - case FR_TYPE_STRUCTURAL: - from = &tlv->vp_group; - break; + fr_pair_t *vp; - default: - fr_assert(0); - } + vp = fr_pair_copy(ctx, tlv); + if (!vp) return -1; + + fr_pair_append(to, vp); + + return 1; } - for (vp = fr_pair_list_head(from); - vp && (cnt < count); - vp = fr_pair_list_next(from, vp)) { - if (!tlv && !fr_dict_attr_common_parent(parent_da, vp->da, true)) continue; - cnt++; + fr_pair_list_foreach(from, vp) { + fr_pair_t *new_vp; + + if (!fr_dict_attr_common_parent(parent_da, vp->da, true)) continue; - PAIR_VERIFY_WITH_LIST(from, vp); new_vp = fr_pair_copy(ctx, vp); if (unlikely(!new_vp)) return -1; + fr_pair_append(to, new_vp); + found = true; } - return cnt; + return found; } /** Duplicate a list of pairs starting at a particular item @@ -2994,6 +2990,8 @@ void fr_pair_verify(char const *file, int line, fr_pair_list_t const *list, fr_p fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: fr_pair_t \"%s\" structural (non-group) type contains itself", file, line, vp->da->name); } + + fr_pair_list_verify(file, line, vp, &vp->vp_group); } switch (vp->vp_type) { diff --git a/src/lib/util/pair.h b/src/lib/util/pair.h index d929582f15d..8e81b5af5ce 100644 --- a/src/lib/util/pair.h +++ b/src/lib/util/pair.h @@ -640,7 +640,7 @@ int fr_pair_list_copy_by_da(TALLOC_CTX *ctx, fr_pair_list_t *to, int fr_pair_list_copy_by_ancestor(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, - fr_dict_attr_t const *parent_da, unsigned int count) CC_HINT(nonnull); + fr_dict_attr_t const *parent_da) CC_HINT(nonnull); int fr_pair_sublist_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, diff --git a/src/lib/util/pair_tests.c b/src/lib/util/pair_tests.c index 5282884aa36..f8446d534ce 100644 --- a/src/lib/util/pair_tests.c +++ b/src/lib/util/pair_tests.c @@ -705,7 +705,7 @@ static void test_fr_pair_list_copy_by_ancestor(void) fr_pair_list_init(&local_pairs); TEST_CASE("Copy 'test_pairs' into 'local_pairs'"); - TEST_CHECK(fr_pair_list_copy_by_ancestor(autofree, &local_pairs, &test_pairs, fr_dict_attr_test_tlv, 0) > 0); + TEST_CHECK(fr_pair_list_copy_by_ancestor(autofree, &local_pairs, &test_pairs, fr_dict_attr_test_tlv) > 0); TEST_CASE("The 'local_pairs' should have only fr_dict_attr_test_tlv_string (ancestor of 'Test-TLV-Root'"); for (vp = fr_pair_dcursor_init(&cursor, &local_pairs); diff --git a/src/process/dhcpv6/base.c b/src/process/dhcpv6/base.c index ae7b087d13d..011be488bf1 100644 --- a/src/process/dhcpv6/base.c +++ b/src/process/dhcpv6/base.c @@ -367,7 +367,7 @@ process_dhcpv6_client_fields_t *dhcpv6_client_fields_store(request_t *request, b * when the structure pairs are nested. */ switch (fr_pair_list_copy_by_ancestor(rctx, &rctx->client_id, - &request->request_pairs, attr_client_id, 0)) { + &request->request_pairs, attr_client_id)) { case -1: REDEBUG("Error copying Client-ID"); error: @@ -383,7 +383,7 @@ process_dhcpv6_client_fields_t *dhcpv6_client_fields_store(request_t *request, b } switch (fr_pair_list_copy_by_ancestor(rctx, &rctx->server_id, - &request->request_pairs, attr_server_id, 0)) { + &request->request_pairs, attr_server_id)) { case -1: REDEBUG("Error copying Server-ID"); goto error;