From: Arran Cudbard-Bell Date: Tue, 23 Jan 2024 22:35:43 +0000 (-0600) Subject: Fix integer (and other) comparisons for switch X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e9fa049ce747fd24b698e03d617a7e7e2591a099;p=thirdparty%2Ffreeradius-server.git Fix integer (and other) comparisons for switch --- diff --git a/src/lib/unlang/compile.c b/src/lib/unlang/compile.c index 2603baabd5d..b5292462738 100644 --- a/src/lib/unlang/compile.c +++ b/src/lib/unlang/compile.c @@ -28,6 +28,7 @@ RCSID("$Id$") #include #include #include +#include #include #include @@ -2991,7 +2992,28 @@ static unlang_t *compile_case(unlang_t *parent, unlang_compile_t *unlang_ctx, CO ssize_t slen; fr_token_t type; unlang_group_t *switch_g; - unlang_switch_t *switch_gext; + unlang_switch_t *switch_gext; + + switch_g = unlang_generic_to_group(parent); + switch_gext = unlang_group_to_switch(switch_g); + + /* + * We need to cast case values to match + * what we're switching over, otherwise + * integers of different widths won't + * match. + */ + t_rules.cast = tmpl_expanded_type(switch_gext->vpt); + + /* + * Need to pass the attribute from switch + * to tmpl rules so we can convert the + * case string to an integer value. + */ + if (tmpl_is_attr(switch_gext->vpt)) { + fr_dict_attr_t const *da = tmpl_attr_tail_da(switch_gext->vpt); + if (da->flags.has_value) t_rules.enumv = da; + } type = cf_section_name2_quote(cs); @@ -3005,8 +3027,7 @@ static unlang_t *compile_case(unlang_t *parent, unlang_compile_t *unlang_ctx, CO return NULL; } - switch_g = unlang_generic_to_group(parent); - switch_gext = unlang_group_to_switch(switch_g); + fr_assert(switch_gext->vpt != NULL); /* diff --git a/src/tests/keywords/switch-integer b/src/tests/keywords/switch-integer new file mode 100644 index 00000000000..2c061dfee38 --- /dev/null +++ b/src/tests/keywords/switch-integer @@ -0,0 +1,11 @@ +&request.REST-HTTP-Status-Code := 200 + +switch &request.REST-HTTP-Status-Code { + case 200 { + success + } + + default { + test_fail + } +}