Skip to content

Commit

Permalink
do more compile-time checks of values.
Browse files Browse the repository at this point in the history
This is only for the modules "delay", "attr_filter", and "exec".
Tho "exec" hasn't been updated yet, as it takes attributes.

These modules should arguably be moved to the call_env framework.
  • Loading branch information
alandekok committed Feb 1, 2025
1 parent 9cd5fca commit 2b8351c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/lib/server/cf_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,25 @@ int cf_pair_parse_value(TALLOC_CTX *ctx, void *out, UNUSED void *base, CONF_ITEM
}
fr_assert(vpt);

/*
* The caller told us what data type was expected. If we do have data, then try to cast
* it to the requested type.
*/
if ((rule->type != FR_TYPE_VOID) && tmpl_contains_data(vpt)) {
slen = 0; // for errors

if (tmpl_is_data_unresolved(vpt)) {
tmpl_cast_set(vpt, rule->type);

if (tmpl_resolve(vpt, NULL) < 0) goto tmpl_error;

} else if (rule->type != tmpl_value_type(vpt)) {
fr_assert(tmpl_is_data(vpt));

if (tmpl_cast_in_place(vpt, rule->type, NULL) < 0) goto tmpl_error;
}
}

*(tmpl_t **)out = vpt;
goto finish;
}
Expand Down
16 changes: 16 additions & 0 deletions src/lib/server/cf_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ _Generic(&(_ct), \
.flags = (_flags), \
.offset = FR_CONF_FLAG_CHECK((_type), (_flags), &(((_struct *)NULL)->_field), offsetof(_struct, _field))

/** conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
*
* This variant takes output hint type. If the type is a bare word, it MUST be of the relevant data type.
*
* @param[in] _name of the CONF_PAIR to search for.
* @param[in] _type to parse the CONF_PAIR as.
* @param[in] _flags controlling parsing behaviour.
* @param[in] _struct containing the field to write the result to.
* @param[in] _field to write the result to.
*/
# define FR_CONF_OFFSET_HINT_TYPE(_name, _type, _struct, _field) \
.name1 = _name, \
.type = (_type), \
.flags = CONF_FLAG_TMPL, \
.offset = FR_CONF_FLAG_CHECK(FR_TYPE_VOID, CONF_FLAG_TMPL, &(((_struct *)NULL)->_field), offsetof(_struct, _field))

/** conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
*
* This variant takes additional flags, and will add CONF_FLAG_MULTI automatically if the field is an array.
Expand Down
2 changes: 2 additions & 0 deletions src/lib/server/tmpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,12 @@ typedef enum tmpl_type_e {
#define tmpl_is_regex_xlat_unresolved(vpt) ((vpt)->type == TMPL_TYPE_REGEX_XLAT_UNRESOLVED)

#define tmpl_needs_resolving(vpt) ((vpt)->type & TMPL_FLAG_UNRESOLVED)
#define tmpl_contains_data(vpt) ((vpt)->type & TMPL_TYPE_DATA)
#define tmpl_contains_attr(vpt) ((vpt)->type & TMPL_FLAG_ATTR)
#define tmpl_contains_regex(vpt) ((vpt)->type & TMPL_FLAG_REGEX)
#define tmpl_contains_xlat(vpt) ((vpt)->type & TMPL_FLAG_XLAT)


extern fr_table_num_ordered_t const tmpl_type_table[];
extern size_t tmpl_type_table_len;

Expand Down

0 comments on commit 2b8351c

Please sign in to comment.