Skip to content

Commit 2b8351c

Browse files
committed
do more compile-time checks of values.
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.
1 parent 9cd5fca commit 2b8351c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/lib/server/cf_parse.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,25 @@ int cf_pair_parse_value(TALLOC_CTX *ctx, void *out, UNUSED void *base, CONF_ITEM
262262
}
263263
fr_assert(vpt);
264264

265+
/*
266+
* The caller told us what data type was expected. If we do have data, then try to cast
267+
* it to the requested type.
268+
*/
269+
if ((rule->type != FR_TYPE_VOID) && tmpl_contains_data(vpt)) {
270+
slen = 0; // for errors
271+
272+
if (tmpl_is_data_unresolved(vpt)) {
273+
tmpl_cast_set(vpt, rule->type);
274+
275+
if (tmpl_resolve(vpt, NULL) < 0) goto tmpl_error;
276+
277+
} else if (rule->type != tmpl_value_type(vpt)) {
278+
fr_assert(tmpl_is_data(vpt));
279+
280+
if (tmpl_cast_in_place(vpt, rule->type, NULL) < 0) goto tmpl_error;
281+
}
282+
}
283+
265284
*(tmpl_t **)out = vpt;
266285
goto finish;
267286
}

src/lib/server/cf_parse.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,22 @@ _Generic(&(_ct), \
244244
.flags = (_flags), \
245245
.offset = FR_CONF_FLAG_CHECK((_type), (_flags), &(((_struct *)NULL)->_field), offsetof(_struct, _field))
246246

247+
/** conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
248+
*
249+
* This variant takes output hint type. If the type is a bare word, it MUST be of the relevant data type.
250+
*
251+
* @param[in] _name of the CONF_PAIR to search for.
252+
* @param[in] _type to parse the CONF_PAIR as.
253+
* @param[in] _flags controlling parsing behaviour.
254+
* @param[in] _struct containing the field to write the result to.
255+
* @param[in] _field to write the result to.
256+
*/
257+
# define FR_CONF_OFFSET_HINT_TYPE(_name, _type, _struct, _field) \
258+
.name1 = _name, \
259+
.type = (_type), \
260+
.flags = CONF_FLAG_TMPL, \
261+
.offset = FR_CONF_FLAG_CHECK(FR_TYPE_VOID, CONF_FLAG_TMPL, &(((_struct *)NULL)->_field), offsetof(_struct, _field))
262+
247263
/** conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
248264
*
249265
* This variant takes additional flags, and will add CONF_FLAG_MULTI automatically if the field is an array.

src/lib/server/tmpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,12 @@ typedef enum tmpl_type_e {
226226
#define tmpl_is_regex_xlat_unresolved(vpt) ((vpt)->type == TMPL_TYPE_REGEX_XLAT_UNRESOLVED)
227227

228228
#define tmpl_needs_resolving(vpt) ((vpt)->type & TMPL_FLAG_UNRESOLVED)
229+
#define tmpl_contains_data(vpt) ((vpt)->type & TMPL_TYPE_DATA)
229230
#define tmpl_contains_attr(vpt) ((vpt)->type & TMPL_FLAG_ATTR)
230231
#define tmpl_contains_regex(vpt) ((vpt)->type & TMPL_FLAG_REGEX)
231232
#define tmpl_contains_xlat(vpt) ((vpt)->type & TMPL_FLAG_XLAT)
232233

234+
233235
extern fr_table_num_ordered_t const tmpl_type_table[];
234236
extern size_t tmpl_type_table_len;
235237

0 commit comments

Comments
 (0)