From: Alan T. DeKok Date: Sun, 17 Apr 2022 16:39:45 +0000 (-0400) Subject: align fr_value_box_network_length() with to/from-network code X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ce80e80182048a3c0b239977f4c65dbc108b7f2;p=thirdparty%2Ffreeradius-server.git align fr_value_box_network_length() with to/from-network code --- diff --git a/src/lib/util/value.c b/src/lib/util/value.c index fe63e621776..4bfede85206 100644 --- a/src/lib/util/value.c +++ b/src/lib/util/value.c @@ -1220,6 +1220,7 @@ int fr_value_box_hton(fr_value_box_t *dst, fr_value_box_t const *src) return 0; } + /** Get the size of the value held by the fr_value_box_t * * This is the length of the NETWORK presentation @@ -1228,8 +1229,35 @@ size_t fr_value_box_network_length(fr_value_box_t const *value) { switch (value->type) { case FR_TYPE_VARIABLE_SIZE: + if (value->enumv) { + /* + * Fixed-width fields. + */ + if (value->enumv->flags.length) { + return value->enumv->flags.length; + } + + /* + * 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; + + } else if (value->enumv->flags.subtype == FLAG_LENGTH_UINT16) { + if (value->vb_length > 65535) return 65535; + } + } + } + return value->vb_length; + + /* + * These can have different encodings, depending on the underlying protocol. + */ case FR_TYPE_DATE: case FR_TYPE_TIME_DELTA: + if (value->enumv) return value->enumv->flags.length; + return value->vb_length; default: @@ -1672,29 +1700,31 @@ ssize_t fr_value_box_from_network(TALLOC_CTX *ctx, /* * Decode fixed-width fields. */ - if (enumv->flags.length) { - newlen = enumv->flags.length; + if (enumv) { + 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; + } else if (da_is_length_field(enumv)) { + /* + * Or fields with a length prefix. + */ + if (enumv->flags.subtype == FLAG_LENGTH_UINT8) { + uint8_t num; - FR_DBUFF_OUT_RETURN(&num, &work_dbuff); - newlen = num; - offset = 1; + FR_DBUFF_OUT_RETURN(&num, &work_dbuff); + newlen = num; + offset = 1; - } else if (enumv->flags.subtype == FLAG_LENGTH_UINT16) { - uint16_t num; + } else if (enumv->flags.subtype == FLAG_LENGTH_UINT16) { + uint16_t num; - FR_DBUFF_OUT_RETURN(&num, &work_dbuff); - newlen = num; - offset = 2; + FR_DBUFF_OUT_RETURN(&num, &work_dbuff); + newlen = num; + offset = 2; - } else { - return -1; + } else { + return -1; + } } }