]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Revise a couple of uses of FR_TYPE_STRUCTURAL
authorJames Jones <jejones3141@gmail.com>
Thu, 16 Nov 2023 20:42:44 +0000 (14:42 -0600)
committerAlan DeKok <aland@freeradius.org>
Mon, 15 Jan 2024 20:08:37 +0000 (15:08 -0500)
FR_TYPE_STRUCTURAL has a name of the same form as the values of
fr_type_t, but is carefully #defined so it can appear in a switch
statement looking like a single value but underneath expanding to
multiple cases, making some of its uses counterintuitive.

src/lib/util/dict_util.c
src/lib/util/types.h
src/lib/util/value.c

index f2f59f9f035b7cca01e85fe1932e2ab2bbbfeca0..f9efb8786f0ce0fa2f30f2518519d81f9153fbe7 100644 (file)
@@ -1114,40 +1114,17 @@ int dict_attr_child_add(fr_dict_attr_t *parent, fr_dict_attr_t *child)
         *      Attributes are inserted into the bin in order of their attribute
         *      numbers to allow slightly more efficient lookups.
         */
-       bin = &children[child->attr & 0xff];
-       for (;;) {
-               bool child_is_struct = false;
-               bool bin_is_struct = false;
-
-               if (!*bin) break;
-
+       for (bin = &children[child->attr & 0xff]; *bin; bin = &(*bin)->next) {
                /*
                 *      Workaround for vendors that overload the RFC space.
                 *      Structural attributes always take priority.
                 */
-               switch (child->type) {
-               case FR_TYPE_STRUCTURAL:
-                       child_is_struct = true;
-                       break;
-
-               default:
-                       break;
-               }
-
-               switch ((*bin)->type) {
-               case FR_TYPE_STRUCTURAL:
-                       bin_is_struct = true;
-                       break;
-
-               default:
-                       break;
-               }
+               bool child_is_struct = fr_type_is_structural(child->type);
+               bool bin_is_struct = fr_type_is_structural((*bin)->type);
 
                if (child_is_struct && !bin_is_struct) break;
-               else if (fr_dict_vendor_num_by_da(child) <= fr_dict_vendor_num_by_da(*bin)) break;      /* Prioritise RFC attributes */
-               else if (child->attr <= (*bin)->attr) break;
-
-               bin = &(*bin)->next;
+               if (fr_dict_vendor_num_by_da(child) <= fr_dict_vendor_num_by_da(*bin)) break;   /* Prioritise RFC attributes */
+               if (child->attr <= (*bin)->attr) break;
        }
 
        memcpy(&this, &bin, sizeof(this));
index d181a771023313f13ceaf2f405950b5e63e7e511..a098e7e1d233a53ec4b6e78973ef3a9358409843 100644 (file)
@@ -217,6 +217,19 @@ typedef enum {
        _mid(FR_TYPE_TLV) \
        _end(FR_TYPE_VENDOR)
 
+/** Hack for truthiness check
+ *
+ * - VSAs
+ * - Structs
+ * - TLVs
+ * - Vendors
+ */
+#define FR_TYPE_STRUCTURAL_EXCEPT_GROUP_DEF(_beg, _mid, _end) \
+       _beg(FR_TYPE_VSA) \
+       _mid(FR_TYPE_STRUCT) \
+       _mid(FR_TYPE_TLV) \
+       _end(FR_TYPE_VENDOR)
+
 /** Match all non value types in case statements
  *
  * - Groups
@@ -279,6 +292,7 @@ typedef enum {
 #define FR_TYPE_QUOTED                         FR_TYPE_QUOTED_DEF(CASE_BEG, CASE_MID, CASE_END)
 
 #define FR_TYPE_STRUCTURAL_EXCEPT_VSA          FR_TYPE_STRUCTURAL_EXCEPT_VSA_DEF(CASE_BEG, CASE_MID, CASE_END)
+#define FR_TYPE_STRUCTURAL_EXCEPT_GROUP                FR_TYPE_STRUCTURAL_EXCEPT_GROUP_DEF(CASE_BEG, CASE_MID, CASE_END)
 #define FR_TYPE_STRUCTURAL                     FR_TYPE_STRUCTURAL_DEF(CASE_BEG, CASE_MID, CASE_END)
 #define FR_TYPE_LEAF                           FR_TYPE_LEAF_DEF(CASE_BEG, CASE_MID, CASE_END)
 #define FR_TYPE_NON_LEAF                       FR_TYPE_NON_LEAF_DEF(CASE_BEG, CASE_MID, CASE_END)
index 1b1c4550b11b069217ce6025edfdecf7f58cf4b1..6689863682fa53f1d75e86d4d453e9dfb47c2255 100644 (file)
@@ -6253,11 +6253,11 @@ bool fr_value_box_is_truthy(fr_value_box_t const *in)
 
        switch (in->type) {
        case FR_TYPE_NULL:
+       case FR_TYPE_STRUCTURAL_EXCEPT_GROUP:
                return false;
 
-       case FR_TYPE_STRUCTURAL:
-               if (in->type == FR_TYPE_GROUP) return (fr_value_box_list_num_elements(&in->vb_group) > 0);
-               return false;
+       case FR_TYPE_GROUP:
+               return (fr_value_box_list_num_elements(&in->vb_group) > 0);
 
        case FR_TYPE_BOOL:
                return in->vb_bool;