]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
more use macros instead of hard-coded field checks
authorAlan T. DeKok <aland@freeradius.org>
Sun, 24 Aug 2025 01:52:33 +0000 (21:52 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Sun, 24 Aug 2025 01:52:33 +0000 (21:52 -0400)
which makes it easier to clean up and rearrange the various
fields in fr_dict_attr_flags_t

src/lib/util/dict_validate.c
src/lib/util/struct.c
src/lib/util/value.c

index 6d12d1cec5118ad32d3c265b1f5b080d5fc73e43..17bc7dad4d065ca8eeb4e649b789d9c114060793 100644 (file)
@@ -171,8 +171,7 @@ bool dict_attr_flags_valid(fr_dict_attr_t *da)
         *      the data type.
         */
        if (flags->extra) {
-               if ((flags->subtype != FLAG_KEY_FIELD) && (flags->subtype != FLAG_BIT_FIELD) &&
-                   (flags->subtype != FLAG_LENGTH_UINT8) && (flags->subtype != FLAG_LENGTH_UINT16)) {
+               if (!fr_dict_attr_is_key_field(da) && !da_is_length_field(da) && !da_is_bit_field(da)) {
                        fr_strerror_const("The 'key' and 'length' flags cannot be used with any other flags.");
                        return false;
                }
@@ -214,7 +213,11 @@ bool dict_attr_flags_valid(fr_dict_attr_t *da)
                        if (flags->array) {
                                ALLOW_FLAG(array);
 
-                               if ((flags->subtype != FLAG_LENGTH_UINT8) && (flags->subtype != FLAG_LENGTH_UINT16)) goto invalid_extra;
+                               if (!da_is_length_field(da)) {
+                                       fr_assert(0);
+                                       goto invalid_extra;
+                               }
+
                        } else if (flags->subtype) {
                        invalid_extra:
                                fr_strerror_const("Invalid type (not 'length=...') for extra flag.");
@@ -226,7 +229,7 @@ bool dict_attr_flags_valid(fr_dict_attr_t *da)
                        break;
 
                case FR_TYPE_STRUCT:
-                       if ((flags->subtype != FLAG_LENGTH_UINT8) && (flags->subtype != FLAG_LENGTH_UINT16)) {
+                       if (!da_is_length_field(da)) {
                                fr_strerror_const("Invalid type (not 'length=...') for extra flag.");
                                return false;
                        }
@@ -248,7 +251,7 @@ bool dict_attr_flags_valid(fr_dict_attr_t *da)
                        return false;
                }
 
-               if (((flags->subtype == FLAG_LENGTH_UINT8) || (flags->subtype == FLAG_LENGTH_UINT16)) &&
+               if (da_is_length_field(da) &&
                    ((type != FR_TYPE_STRING) && (type != FR_TYPE_OCTETS) && (type != FR_TYPE_STRUCT))) {
                        fr_strerror_printf("The 'length' flag cannot be used used with type %s",
                                           fr_type_to_str(type));
index 1fe85b1d71be33c6cf44047ab42abf1ca081d57f..28be9a758f599ab1b38ffa379847c48908792d72 100644 (file)
@@ -123,9 +123,10 @@ ssize_t fr_struct_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out,
                /*
                 *      Set how many bytes there are in the "length" field.
                 */
-               if (parent->flags.subtype == FLAG_LENGTH_UINT8) {
+               if (da_is_length_field8(parent)) {
                        field_len = 1;
                } else {
+                       fr_assert(da_is_length_field16(parent));
                        field_len = 2;
                }
 
@@ -606,20 +607,23 @@ ssize_t fr_struct_to_network(fr_dbuff_t *dbuff,
         */
        if (!da_is_length_field(parent)) {
                work_dbuff = FR_DBUFF(dbuff);
+
+       } else if (da_is_length_field8(parent)) {
+               work_dbuff = FR_DBUFF_MAX(dbuff, UINT8_MAX);
+               fr_dbuff_marker(&hdr, &work_dbuff);
+
+               FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 1);
+               prefix_length = 1;
+               do_length = true;
+
        } else {
-               if (parent->flags.subtype == FLAG_LENGTH_UINT8) {
-                       work_dbuff = FR_DBUFF_MAX(dbuff, 256);
-                       fr_dbuff_marker(&hdr, &work_dbuff);
+               fr_assert(da_is_length_field16(parent));
 
-                       FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 1);
-                       prefix_length = 1;
-               } else {
-                       work_dbuff = FR_DBUFF_MAX(dbuff, 65536);
-                       fr_dbuff_marker(&hdr, &work_dbuff);
+               work_dbuff = FR_DBUFF_MAX(dbuff, UINT16_MAX);
+               fr_dbuff_marker(&hdr, &work_dbuff);
 
-                       FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 2);
-                       prefix_length = 2;
-               }
+               FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 2);
+               prefix_length = 2;
                do_length = true;
        }
 
@@ -905,7 +909,7 @@ done:
                 */
                if (length < prefix_length) return PAIR_ENCODE_FATAL_ERROR;
 #endif
-               if (parent->flags.subtype == FLAG_LENGTH_UINT8) {
+               if (da_is_length_field8(parent)) {
                        length -= prefix_length;
 
                        length += da_length_offset(parent);
index 393de99c9c56acfe5f4725e2ee8f8e71bbc11211..6e7a5a96af69da0dfc262e04584623e9092ac781 100644 (file)
@@ -1442,13 +1442,11 @@ size_t fr_value_box_network_length(fr_value_box_t const *value)
                        /*
                         *      Clamp length at maximum we're allowed to encode.
                         */
-                       if (da_is_length_field(value->enumv)) {
-                               if (value->enumv->flags.subtype == FLAG_LENGTH_UINT8) {
-                                       if (value->vb_length > 255) return 255;
+                       if (da_is_length_field8(value->enumv)) {
+                               if (value->vb_length > UINT8_MAX) return UINT8_MAX;
 
-                               } else if (value->enumv->flags.subtype == FLAG_LENGTH_UINT16) {
-                                       if (value->vb_length > 65535) return 65535;
-                               }
+                       } else if (da_is_length_field16(value->enumv)) {
+                               if (value->vb_length > UINT16_MAX) return UINT16_MAX;
                        }
                }
                return value->vb_length;
@@ -1560,21 +1558,17 @@ ssize_t fr_value_box_to_network(fr_dbuff_t *dbuff, fr_value_box_t const *value)
                                        max = value->enumv->flags.length;
                                }
 
-                       } else if (da_is_length_field(value->enumv)) {
+                       } else if (da_is_length_field8(value->enumv)) {
                                /*
                                 *      Truncate the output to the max allowed for this field and encode the length.
                                 */
-                               if (value->enumv->flags.subtype == FLAG_LENGTH_UINT8) {
-                                       if (max > 255) max = 255;
-                                       FR_DBUFF_IN_RETURN(&work_dbuff, (uint8_t) max);
+                               if (max > UINT8_MAX) max = UINT8_MAX;
+                               FR_DBUFF_IN_RETURN(&work_dbuff, (uint8_t) max);
 
-                               } else if (value->enumv->flags.subtype == FLAG_LENGTH_UINT16) {
-                                       if (max > 65536) max = 65535;
-                                       FR_DBUFF_IN_RETURN(&work_dbuff, (uint16_t) max);
+                       } else if (da_is_length_field16(value->enumv)) {
 
-                               } else {
-                                       return -1;
-                               }
+                               if (max > UINT16_MAX) max = UINT16_MAX;
+                               FR_DBUFF_IN_RETURN(&work_dbuff, (uint16_t) max);
                        }
                }
 
@@ -1966,27 +1960,19 @@ ssize_t fr_value_box_from_network(TALLOC_CTX *ctx,
                        if (enumv->flags.length) {
                                newlen = enumv->flags.length;
 
-                       } else if (da_is_length_field(enumv)) {
-                               /*
-                                *      Or fields with a length prefix.
-                                */
-                               if (enumv->flags.subtype == FLAG_LENGTH_UINT8) {
-                                       uint8_t num = 0;
-
-                                       FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
-                                       newlen = num;
-                                       offset = 1;
+                       } else if (da_is_length_field8(enumv)) {
+                               uint8_t num = 0;
 
-                               } else if (enumv->flags.subtype == FLAG_LENGTH_UINT16) {
-                                       uint16_t num = 0;
+                               FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
+                               newlen = num;
+                               offset = 1;
 
-                                       FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
-                                       newlen = num;
-                                       offset = 2;
+                       } else if (da_is_length_field16(enumv)) {
+                               uint16_t num = 0;
 
-                               } else {
-                                       return -1;
-                               }
+                               FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
+                               newlen = num;
+                               offset = 2;
                        }
                }