]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
don't lose casts when they're explicitly given
authorAlan T. DeKok <aland@freeradius.org>
Tue, 31 Jan 2023 21:45:23 +0000 (16:45 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 1 Feb 2023 01:05:37 +0000 (20:05 -0500)
(foo)%{xlat..}

was being set to just

%{xlat...}

and that's wrong.  :(

src/lib/unlang/xlat_expr.c
src/tests/unit/xlat/cond_base.txt
src/tests/unit/xlat/expr.txt
src/tests/unit/xlat/purify.txt

index c078cf177faf31d733322f581bb95de4554c1c9e..4ab5f739366ac644b5b06f521ddebd286f4845ac 100644 (file)
@@ -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
index 839c711d8530a6a734795a6381aec2013ba199f1..258e4c7685876427a3ff55e322913cab83d536e6 100644 (file)
@@ -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")
index d3127b2371e6d79f84f37d7e1c25bcce2b02e1ac..a99d9c70a268fc37ad9681597360e2731d583f34 100644 (file)
@@ -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
index 500123f490a99f30724c6ff47fc9c5c6e8a716e0..eb2cc6dd95433f820e235d380be8efd84debdc29 100644 (file)
@@ -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