]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
remove duplicate casts
authorAlan T. DeKok <aland@freeradius.org>
Mon, 2 Sep 2024 18:01:55 +0000 (14:01 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 2 Sep 2024 21:38:58 +0000 (17:38 -0400)
src/lib/unlang/xlat_expr.c
src/tests/unit/condition/base.txt
src/tests/unit/xlat/cond_base.txt
src/tests/unit/xlat/purify.txt

index 26cb49fb02325db7ee705b013c5b05a1b1e0132d..c6a03689614c9c8ba9472525a58fe01b7b970f0f 100644 (file)
@@ -2634,6 +2634,19 @@ static fr_slen_t tokenize_field(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuf
                                cast_type = FR_TYPE_NULL;
                        }
                }
+
+               /*
+                *      Push the cast down.
+                *
+                *      But if we're casting to string, and the RHS is already a string, we don't need to cast
+                *      it.  We can just discard the cast.
+                */
+               if ((cast_type != FR_TYPE_NULL) && (tmpl_rules_cast(vpt) == FR_TYPE_NULL)) {
+                       if ((cast_type != FR_TYPE_STRING) || (vpt->quote == T_BARE_WORD)) {
+                               tmpl_cast_set(vpt, cast_type);
+                       }
+                       cast_type = FR_TYPE_NULL;
+               }
        }
 
        /*
index b8066a972aed71c4321ec62336f6a59cb18446f4..fdb3adeb2a6d109bebaaabf3281159c5db3d8fd6 100644 (file)
@@ -224,7 +224,7 @@ match  (&Session-Timeout == '10')
 # Except for dates, which can be humanly readable!
 # This one is be an expansion, so it's left as-is.
 condition &Event-Timestamp == "January 1, 2012 %{blah}"
-match (&Event-Timestamp == %cast(string, "January 1, 2012 %{blah}"))
+match (&Event-Timestamp == "January 1, 2012 %{blah}")
 
 # This one is NOT an expansion, so it's parsed into normal form
 condition &Event-Timestamp == 'January 1, 2012'
@@ -248,7 +248,7 @@ condition (ipaddr)127.0.0.1 == "127.0.0.1"
 match true
 
 condition (ipaddr)127.0.0.1 == "%md4(' 127.0.0.1')"
-match (127.0.0.1 == %cast(string, "%md4(' 127.0.0.1')"))
+match (127.0.0.1 == "%md4(' 127.0.0.1')")
 
 #
 #  Bare %{...} is allowed.
@@ -263,7 +263,7 @@ 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 (00:11:22:33:44:55 == %cast(string, "%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 ':'
@@ -317,10 +317,10 @@ condition ('foo' == 'foo')
 match true
 
 condition ("foo" == "%md4(' foo')")
-match ("foo" == %cast(string, "%md4(' foo')"))
+match ("foo" == "%md4(' foo')")
 
 condition ("foo bar" == "%md4(' foo')")
-match ("foo bar" == %cast(string, "%md4(' foo')"))
+match ("foo bar" == "%md4(' foo')")
 
 condition ("foo" == "bar")
 match false
@@ -337,7 +337,7 @@ condition (&User-Name == "bob")
 match (&User-Name == "bob")
 
 condition (&User-Name == "%md4(' blah')")
-match (&User-Name == %cast(string, "%md4(' blah')"))
+match (&User-Name == "%md4(' blah')")
 
 condition (ipaddr)127.0.0.1 == 2130706433
 match true
@@ -422,7 +422,7 @@ match ("foo" == &User-Name)
 
 # This used to be expr, but expr isn't a builtin, so it failed...
 condition (integer)"%md4(' 1 + 1')" < &NAS-Port
-match (%cast(uint32, "%md4(' 1 + 1')") < &NAS-Port)
+match ((uint32)"%md4(' 1 + 1')" < &NAS-Port)
 
 #
 #  The string gets parsed as an IP address.
@@ -456,7 +456,7 @@ match ((ipaddr)&PMIP6-Home-IPv4-HoA == &Framed-IP-Address)
 
 # but these are allowed
 condition (ether)&Acct-Input-Octets64 == "%interpreter('foo')"
-match ((ether)&Acct-Input-Octets64 == %cast(string, "%interpreter('foo')"))
+match ((ether)&Acct-Input-Octets64 == "%interpreter('foo')")
 
 condition (ipaddr)&Filter-Id == &Framed-IP-Address
 match ((ipaddr)&Filter-Id == &Framed-IP-Address)
@@ -492,11 +492,11 @@ match &Foo-Bar
 #      data &Acct-Input-Octets > &Session-Timeout
 #
 condition &Acct-Input-Octets > "%{Session-Timeout}"
-match (&Acct-Input-Octets > %cast(string, "%{Session-Timeout}"))
+match (&Acct-Input-Octets > "%{Session-Timeout}")
 
 #  Separate types aren't optimized
 condition &Acct-Input-Octets-64 > "%{Session-Timeout}"
-match (&Acct-Input-Octets-64 > %cast(string, "%{Session-Timeout}"))
+match (&Acct-Input-Octets-64 > "%{Session-Timeout}")
 
 #
 #  Parse OIDs into known attributes, where possible.
index ea2f1d64479f7d28cfb111bc19dfef00cff00e77..bfefd84f20c9f6cea84f68d963d12ec3dcbe0e8d 100644 (file)
@@ -222,7 +222,7 @@ match (&Session-Timeout == '10')
 # This one is be an expansion, so it's left as-is.
 #
 xlat_purify &Event-Timestamp == "January 1, 2012 %{User-Name}"
-match (&Event-Timestamp == %cast(string, "January 1, 2012 %{User-Name}"))
+match (&Event-Timestamp == "January 1, 2012 %{User-Name}")
 
 # This one is NOT an expansion, so it's parsed into normal form
 xlat_purify &Event-Timestamp == 'January 1 2012'
@@ -250,7 +250,7 @@ match true
 # LHS is IPaddr, RHS is string (malformed IP address).
 # We can only fail this at run-time.
 xlat_purify (ipaddr)127.0.0.1 == "%md4(' 127.0.0.1')"
-match (127.0.0.1 == %cast(string, "%md4(' 127.0.0.1')"))
+match (127.0.0.1 == "%md4(' 127.0.0.1')")
 
 #
 #  Bare %{...} is allowed.
@@ -268,7 +268,7 @@ match true
 
 # Invalid cast from octets to ether.
 xlat_purify (ether)00:11:22:33:44:55 == "%md4('00:11:22:33:44:55')"
-match (00:11:22:33:44:55 == %cast(string, "%md4('00:11:22:33:44:55')"))
+match (00:11:22:33:44:55 == "%md4('00:11:22:33:44:55')")
 
 xlat_purify (ether) 00:XX:22:33:44:55 == 00:11:22:33:44:55
 match ERROR offset 12: Missing separator, expected ':'
@@ -430,7 +430,7 @@ match ("foo" == &User-Name)
 #  ERROR: Failed casting 0x002ade8665c69219ca16bd108d92c8d5 to data type uint32: Invalid cast from octets to uint32.  Source length 16 is greater than destination type size 4
 #
 xlat_purify (integer)"%md4(' 1 + 1')" < &NAS-Port
-match (%cast(uint32, "%md4(' 1 + 1')") < &NAS-Port)
+match ((uint32)"0x002ade8665c69219ca16bd108d92c8d5" < &NAS-Port)
 
 #
 #  The string gets parsed as an IP address.
@@ -463,7 +463,7 @@ match ((ipaddr)&PMIP6-Home-IPv4-HoA == &Framed-IP-Address)
 
 # but these are allowed
 xlat_purify (ether)&Acct-Input-Octets64 == "%interpreter('foo')"
-match  ((ether)&Acct-Input-Octets64 == %cast(string, "%interpreter('foo')"))
+match  ((ether)&Acct-Input-Octets64 == "%interpreter('foo')")
 
 xlat_purify (ipaddr)&Filter-Id == &Framed-IP-Address
 match ((ipaddr)&Filter-Id == &Framed-IP-Address)
@@ -492,7 +492,7 @@ match (&User-Name[n] == "bob")
 #match &Foo-Bar
 
 xlat_purify &Acct-Input-Octets > "%{Session-Timeout}"
-match (&Acct-Input-Octets > %cast(string, "%{Session-Timeout}"))
+match (&Acct-Input-Octets > "%{Session-Timeout}")
 
 xlat_purify &Acct-Input-Octets > &Session-Timeout
 match (&Acct-Input-Octets > &Session-Timeout)
index b5abf32a98ec6a3f4b246e21eb2f5ba19b6bcf15..c13d980418965c675ad71e72eed267ff733ebc86 100644 (file)
@@ -223,7 +223,7 @@ match "hello 3 bob"
 #  The real run-time tests work
 #
 xlat_purify "hello %{1 + 2} bob"
-match %cast(string, "hello %{(1 + 2)} bob")
+match "hello %{(1 + 2)} bob"
 
 #
 #  New syntax!