From: Alan T. DeKok Date: Mon, 2 Sep 2024 18:01:55 +0000 (-0400) Subject: remove duplicate casts X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49f6714a519d9400f4d4cc1bbac527e66dddfa9e;p=thirdparty%2Ffreeradius-server.git remove duplicate casts --- diff --git a/src/lib/unlang/xlat_expr.c b/src/lib/unlang/xlat_expr.c index 26cb49fb02..c6a0368961 100644 --- a/src/lib/unlang/xlat_expr.c +++ b/src/lib/unlang/xlat_expr.c @@ -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; + } } /* diff --git a/src/tests/unit/condition/base.txt b/src/tests/unit/condition/base.txt index b8066a972a..fdb3adeb2a 100644 --- a/src/tests/unit/condition/base.txt +++ b/src/tests/unit/condition/base.txt @@ -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. diff --git a/src/tests/unit/xlat/cond_base.txt b/src/tests/unit/xlat/cond_base.txt index ea2f1d6447..bfefd84f20 100644 --- a/src/tests/unit/xlat/cond_base.txt +++ b/src/tests/unit/xlat/cond_base.txt @@ -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) diff --git a/src/tests/unit/xlat/purify.txt b/src/tests/unit/xlat/purify.txt index b5abf32a98..c13d980418 100644 --- a/src/tests/unit/xlat/purify.txt +++ b/src/tests/unit/xlat/purify.txt @@ -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!