]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
update fr_pair_list_copy_by_ancestor()
authorAlan T. DeKok <aland@freeradius.org>
Fri, 14 Jul 2023 20:24:35 +0000 (16:24 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 14 Jul 2023 21:03:28 +0000 (17:03 -0400)
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"

src/lib/util/pair.c
src/lib/util/pair.h
src/lib/util/pair_tests.c
src/process/dhcpv6/base.c

index 27867ebab4041ce9bda42fbc552fd31e989d1bd3..2707f95dbbd515e75e863a32ac046372e9c8d5fa 100644 (file)
@@ -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) {
index d929582f15d1ebf55cc4d9ca91c1e00561c2e24d..8e81b5af5ce635429f67752f26480dfaff50fb4f 100644 (file)
@@ -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,
index 5282884aa36317e39a7a22e2f8c00c10b388c496..f8446d534ce776a1d962e233bb0c8d8c75b6f00f 100644 (file)
@@ -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);
index ae7b087d13d4065b7eb82d8f73af07cc82ed5bd0..011be488bf14061d9eb18832148ecec8d3280fe9 100644 (file)
@@ -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;