]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
suppress useless casts.
authorAlan T. DeKok <aland@freeradius.org>
Thu, 26 May 2022 14:47:21 +0000 (10:47 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 3 Jun 2022 11:15:41 +0000 (07:15 -0400)
src/lib/server/tmpl_tokenize.c
src/tests/unit/condition/base.txt
src/tests/unit/condition/casts.txt
src/tests/unit/condition/ip.txt

index ce1f73dcb587f50f4ed728ef1b7697a4cd28e515..e827babd8a60f05af9e4591621544d62786ef3f7 100644 (file)
@@ -1686,8 +1686,9 @@ do_suffix:
                tmpl_attr_insert(vpt, ar);
        }
 
-       fr_sbuff_marker_release(&m_s);
+       if (tmpl_is_attr(vpt) && (tmpl_rules_cast(vpt) == tmpl_da(vpt)->type)) vpt->rules.cast = FR_TYPE_NULL;
 
+       fr_sbuff_marker_release(&m_s);
        return 0;
 }
 
@@ -2040,6 +2041,7 @@ ssize_t tmpl_afrom_attr_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err,
 
        tmpl_set_name(vpt, T_BARE_WORD, fr_sbuff_start(&our_name), fr_sbuff_used(&our_name));
        vpt->rules = *t_rules;  /* Record the rules */
+       if (tmpl_is_attr(vpt) && (tmpl_da(vpt)->type == tmpl_rules_cast(vpt))) vpt->rules.cast = FR_TYPE_NULL;
 
        if (!tmpl_substr_terminal_check(&our_name, p_rules)) {
                fr_strerror_const("Unexpected text after attribute reference");
@@ -2149,6 +2151,8 @@ static fr_slen_t tmpl_afrom_value_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff
 
        *out = vpt;
 
+       if (tmpl_rules_cast(vpt) == tmpl_value_type(vpt)) vpt->rules.cast = FR_TYPE_NULL;
+
        TMPL_VERIFY(vpt);
 
        return fr_sbuff_set(in, &our_in);
@@ -3337,6 +3341,11 @@ int tmpl_cast_in_place(tmpl_t *vpt, fr_type_t type, fr_dict_attr_t const *enumv)
                vpt->quote = tmpl_cast_quote(vpt->quote, type, enumv,
                                             unescaped, talloc_array_length(unescaped) - 1);
                talloc_free(unescaped);
+
+               /*
+                *      The data is now of the correct type, so we don't need to keep a cast.
+                */
+               vpt->rules.cast = FR_TYPE_NULL;
        }
                break;
 
@@ -3362,10 +3371,24 @@ int tmpl_cast_in_place(tmpl_t *vpt, fr_type_t type, fr_dict_attr_t const *enumv)
                } else {
                        vpt->quote = T_BARE_WORD;
                }
+
+               /*
+                *      The data is now of the correct type, so we don't need to keep a cast.
+                */
+               vpt->rules.cast = FR_TYPE_NULL;
        }
                break;
 
-       case TMPL_TYPE_ATTR:    /* FIXME - We should check cast compatibility with resolved attrs */
+       case TMPL_TYPE_ATTR:
+               /*
+                *      Suppress casts to the same type.
+                */
+               if (tmpl_da(vpt)->type == type) {
+                       vpt->rules.cast = FR_TYPE_NULL;
+                       break;
+               }
+               FALL_THROUGH;
+
        case TMPL_TYPE_ATTR_UNRESOLVED:
                vpt->rules.cast = type;
                break;
@@ -3626,7 +3649,15 @@ int tmpl_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules)
         *      The attribute reference needs resolving.
         */
        } else if (tmpl_contains_attr(vpt)) {
+               fr_type_t               dst_type = tmpl_rules_cast(vpt);
+
                ret = tmpl_attr_resolve(vpt, tr_rules);
+               if (ret < 0) return ret;
+
+               if (dst_type == tmpl_da(vpt)->type) {
+                       vpt->rules.cast = FR_TYPE_NULL;
+               }
+
 
        /*
         *      Convert unresolved tmpls int enumvs, or failing that, string values.
index b9b8e4e98804d07a094a013a2a160bff8ed6a201..bdb3fb7b91207370a17f508ab10691b7d1a8eb4f 100644 (file)
@@ -132,7 +132,7 @@ condition !&User-Name != &User-Password
 match &User-Name == &User-Password
 
 condition <ipv6addr>::1
-match ERROR offset 0: Cannot do cast for existence check
+match ERROR offset 0: Invalid cast from ipv6addr to bool.  Unsupported
 
 condition <ipaddr>&Filter-Id == &Framed-IP-Address
 match <ipaddr>&Filter-Id == &Framed-IP-Address
@@ -154,7 +154,7 @@ match ERROR offset 1: Unknown data type
 #  Normalize things
 #
 condition <ipaddr>127.0.0.1 < &Framed-IP-Address
-match &Framed-IP-Address > <ipaddr>127.0.0.1
+match &Framed-IP-Address > 127.0.0.1
 
 # =* and !* are only for attrs / lists
 condition "foo" !* bar
@@ -232,22 +232,22 @@ condition <ipaddr>127.0.0.1 == "127.0.0.1"
 match true
 
 condition <ipaddr>127.0.0.1 == "%{md4: 127.0.0.1}"
-match <ipaddr>127.0.0.1 == "%{md4: 127.0.0.1}"
+match 127.0.0.1 == "%{md4: 127.0.0.1}"
 
 #
 #  Bare %{...} is allowed.
 #
 condition <ipaddr>127.0.0.1 == %{md4:127.0.0.1}
-match <ipaddr>127.0.0.1 == %{md4:127.0.0.1}
+match 127.0.0.1 == %{md4:127.0.0.1}
 
 condition <ipaddr>127.0.0.1 == %{md4: SELECT user FROM table WHERE user='%{User-Name}'}
-match <ipaddr>127.0.0.1 == %{md4: SELECT user FROM table WHERE user='%{User-Name}'}
+match 127.0.0.1 == %{md4: SELECT user FROM table WHERE user='%{User-Name}'}
 
 condition <ether> 00:11:22:33:44:55 == "00:11:22:33:44:55"
 match true
 
 condition <ether>00:11:22:33:44:55 == "%{md4:00:11:22:33:44:55}"
-match <ether>00:11:22:33:44:55 == "%{md4:00:11:22:33:44:55}"
+match 00:11:22:33:44:55 == "%{md4:00:11:22:33:44:55}"
 
 condition <ether> 00:XX:22:33:44:55 == 00:11:22:33:44:55
 match ERROR offset 12: Missing separator, expected ':'
@@ -405,7 +405,7 @@ match ERROR offset 2: Forbidden data type 'vsa' in cast
 #  is on the LHS of the condition.
 #
 condition <string>"foo" == &User-Name
-match &User-Name == <string>"foo"
+match &User-Name == "foo"
 
 # This used to be expr, but expr isn't a builtin, so it failed...
 condition <integer>"%{md4: 1 + 1}" < &NAS-Port
@@ -418,7 +418,7 @@ condition &Filter-Id == &Framed-IP-Address
 match <ipaddr>&Filter-Id == &Framed-IP-Address
 
 condition <ipaddr>127.0.0.1 == &Filter-Id
-match <ipaddr>&Filter-Id == <ipaddr>127.0.0.1
+match <ipaddr>&Filter-Id == 127.0.0.1
 
 condition &Tmp-uint64-0 == &request.Foo-Stuff-Bar
 match &Tmp-uint64-0 == &Foo-Stuff-Bar
@@ -594,7 +594,7 @@ match <ipv4prefix>&NAS-IP-Address == 192.168.0.0/24
 #  is parsed as ipv4prefix
 #
 condition <ipv4prefix>192.168.0.0/24 > &NAS-IP-Address
-match <ipv4prefix>&NAS-IP-Address < <ipv4prefix>192.168.0.0/24
+match <ipv4prefix>&NAS-IP-Address < 192.168.0.0/24
 
 #
 #  This is allowed and means "the list is not empty"
index 07cada746f3d3cbf20f4574b4cf3fdb170f8a402..6a91465a89a203e24718fb68089e369338aad479 100644 (file)
@@ -9,11 +9,11 @@ tmpl-rules allow_unresolved=yes allow_unknown=yes
 
 # Forcefully cast RHS bareword
 condition &User-Name == <ipaddr>192.168.0.1
-match <ipaddr>&User-Name == <ipaddr>192.168.0.1
+match <ipaddr>&User-Name == 192.168.0.1
 
 # Forcefully cast LHS bareword
 condition <ipaddr>192.168.0.1 == &User-Name
-match <ipaddr>&User-Name == <ipaddr>192.168.0.1
+match <ipaddr>&User-Name == 192.168.0.1
 
 # Forcefully cast RHS single quotes
 #condition &Framed-IP-Address == <ipaddr>'192.168.0.1'
index 4d664ba4cbecebfdcf29cd10c8bcbed5cee51c5e..7ca6df7a6aa22b042d5b02f4fec64f58d6a8aa53 100644 (file)
@@ -11,7 +11,7 @@ condition &Packet-Src-IPv6-Address == ::
 match &Packet-Src-IPv6-Address == ::
 
 condition &Packet-Src-IP-Address == <ipaddr>*
-match &Packet-Src-IP-Address == <ipaddr>0.0.0.0
+match &Packet-Src-IP-Address == 0.0.0.0
 
 condition &Packet-Src-IP-Address == 0.0.0.0
 match &Packet-Src-IP-Address == 0.0.0.0