From: Alan T. DeKok Date: Tue, 31 Jan 2023 21:45:23 +0000 (-0500) Subject: don't lose casts when they're explicitly given X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=71d485d3e81998ff813d6ede7d18616109085aad;p=thirdparty%2Ffreeradius-server.git don't lose casts when they're explicitly given (foo)%{xlat..} was being set to just %{xlat...} and that's wrong. :( --- diff --git a/src/lib/unlang/xlat_expr.c b/src/lib/unlang/xlat_expr.c index c078cf177fa..4ab5f739366 100644 --- a/src/lib/unlang/xlat_expr.c +++ b/src/lib/unlang/xlat_expr.c @@ -2322,11 +2322,10 @@ static fr_slen_t tokenize_field(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuf if (tmpl_is_xlat(vpt) || tmpl_is_xlat_unresolved(vpt)) { xlat_exp_head_t *xlat = tmpl_xlat(vpt); xlat_exp_t *cast; - fr_type_t type; + fr_type_t type = FR_TYPE_NULL; talloc_steal(node, xlat); node->fmt = talloc_typed_strdup(node, vpt->name); - talloc_free(vpt); xlat_exp_set_type(node, XLAT_GROUP); node->quote = quote; @@ -2334,30 +2333,35 @@ static fr_slen_t tokenize_field(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuf node->flags = xlat->flags; - if (quote != T_BARE_WORD) { - if (tmpl_rules_cast(vpt) != FR_TYPE_NULL) { - type = tmpl_rules_cast(vpt); - - } else { - type = FR_TYPE_STRING; - - /* - * The string is T_DOUBLE_QUOTED_STRING, or T_BACK_QUOTED_STRING, - * both of which have double-quoting rules. - * - * 'foo' can't contain an xlat. - * /foo/ is forbidden, as we don't expect a regex here. - * - * We rely on xlat_func_cast() to see the `string` cast, and then to - * _print_ the result to a string. - */ - } + /* + * Enforce a cast type. + */ + if (tmpl_rules_cast(vpt) != FR_TYPE_NULL) { + type = tmpl_rules_cast(vpt); + + } else if (quote != T_BARE_WORD) { + type = FR_TYPE_STRING; + /* + * The string is T_DOUBLE_QUOTED_STRING, or T_BACK_QUOTED_STRING, + * both of which have double-quoting rules. + * + * 'foo' can't contain an xlat. + * /foo/ is forbidden, as we don't expect a regex here. + * + * We rely on xlat_func_cast() to see the `string` cast, and then to + * _print_ the result to a string. + */ + } + + if (type != FR_TYPE_NULL) { MEM(cast = expr_cast_alloc(head, type)); xlat_func_append_arg(cast, node, false); node = cast; } + talloc_free(vpt); + } else { /* * Else we're not hoisting, set the node to the VPT diff --git a/src/tests/unit/xlat/cond_base.txt b/src/tests/unit/xlat/cond_base.txt index 839c711d853..258e4c76858 100644 --- a/src/tests/unit/xlat/cond_base.txt +++ b/src/tests/unit/xlat/cond_base.txt @@ -355,7 +355,7 @@ xlat_purify (&User-Name == %{md4: blah}) match (&User-Name == 0x544924d05ec4481925ba3749a096a0a7) xlat_purify (&User-Name == (string) %{md4: blah}) -match (&User-Name == 0x544924d05ec4481925ba3749a096a0a7) +match (&User-Name == "0x544924d05ec4481925ba3749a096a0a7") xlat_purify (&User-Name == "%{md4: blah}") match (&User-Name == "0x544924d05ec4481925ba3749a096a0a7") diff --git a/src/tests/unit/xlat/expr.txt b/src/tests/unit/xlat/expr.txt index d3127b2371e..a99d9c70a26 100644 --- a/src/tests/unit/xlat/expr.txt +++ b/src/tests/unit/xlat/expr.txt @@ -111,7 +111,7 @@ xlat_expr 1 < 2 < 3 match ((1 < 2) < 3) xlat_expr (uint32) %(concat:1 2) -match %(concat:1 2) +match %(cast:uint32 %(concat:1 2)) # # Mashing multiple brackets together. The brackets are removed as diff --git a/src/tests/unit/xlat/purify.txt b/src/tests/unit/xlat/purify.txt index 500123f490a..eb2cc6dd954 100644 --- a/src/tests/unit/xlat/purify.txt +++ b/src/tests/unit/xlat/purify.txt @@ -190,5 +190,35 @@ match ERROR offset 6: Invalid operation on module return code xlat_purify !fail match !%{rcode:'fail'} +# +# Casts and such +# +xlat_purify (string)(%{expr:1}) +match "1" + +# +# This is a different code path than the above. +# +xlat_purify (string)%{expr:1} +match "1" + +xlat_purify "hello" +match "hello" + +# +# String concatenation +# +xlat_purify "hello " + (string)%{expr:1} +match "hello 1" + +xlat_purify "hello " + (string)%{expr:1} + " bob" +match "hello 1 bob" + +# +# This should be the same as above, but it isn't! +# +#xlat_purify "hello %{expr:1} bob" +#match "hello 1 bob" + count -match 85 +match 95