From: Alan T. DeKok Date: Thu, 26 May 2022 14:47:21 +0000 (-0400) Subject: suppress useless casts. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d89d28210e7fb0aa2e738a83ffb353f05c7c3f9;p=thirdparty%2Ffreeradius-server.git suppress useless casts. --- diff --git a/src/lib/server/tmpl_tokenize.c b/src/lib/server/tmpl_tokenize.c index ce1f73dcb58..e827babd8a6 100644 --- a/src/lib/server/tmpl_tokenize.c +++ b/src/lib/server/tmpl_tokenize.c @@ -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. diff --git a/src/tests/unit/condition/base.txt b/src/tests/unit/condition/base.txt index b9b8e4e9880..bdb3fb7b912 100644 --- a/src/tests/unit/condition/base.txt +++ b/src/tests/unit/condition/base.txt @@ -132,7 +132,7 @@ condition !&User-Name != &User-Password match &User-Name == &User-Password condition ::1 -match ERROR offset 0: Cannot do cast for existence check +match ERROR offset 0: Invalid cast from ipv6addr to bool. Unsupported condition &Filter-Id == &Framed-IP-Address match &Filter-Id == &Framed-IP-Address @@ -154,7 +154,7 @@ match ERROR offset 1: Unknown data type # Normalize things # condition 127.0.0.1 < &Framed-IP-Address -match &Framed-IP-Address > 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 127.0.0.1 == "127.0.0.1" match true condition 127.0.0.1 == "%{md4: 127.0.0.1}" -match 127.0.0.1 == "%{md4: 127.0.0.1}" +match 127.0.0.1 == "%{md4: 127.0.0.1}" # # Bare %{...} is allowed. # condition 127.0.0.1 == %{md4:127.0.0.1} -match 127.0.0.1 == %{md4:127.0.0.1} +match 127.0.0.1 == %{md4:127.0.0.1} condition 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}'} +match 127.0.0.1 == %{md4: SELECT user FROM table WHERE user='%{User-Name}'} condition 00:11:22:33:44:55 == "00:11:22:33:44:55" match true condition 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}" +match 00:11:22:33:44:55 == "%{md4:00:11:22:33:44:55}" condition 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 "foo" == &User-Name -match &User-Name == "foo" +match &User-Name == "foo" # This used to be expr, but expr isn't a builtin, so it failed... condition "%{md4: 1 + 1}" < &NAS-Port @@ -418,7 +418,7 @@ condition &Filter-Id == &Framed-IP-Address match &Filter-Id == &Framed-IP-Address condition 127.0.0.1 == &Filter-Id -match &Filter-Id == 127.0.0.1 +match &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 &NAS-IP-Address == 192.168.0.0/24 # is parsed as ipv4prefix # condition 192.168.0.0/24 > &NAS-IP-Address -match &NAS-IP-Address < 192.168.0.0/24 +match &NAS-IP-Address < 192.168.0.0/24 # # This is allowed and means "the list is not empty" diff --git a/src/tests/unit/condition/casts.txt b/src/tests/unit/condition/casts.txt index 07cada746f3..6a91465a89a 100644 --- a/src/tests/unit/condition/casts.txt +++ b/src/tests/unit/condition/casts.txt @@ -9,11 +9,11 @@ tmpl-rules allow_unresolved=yes allow_unknown=yes # Forcefully cast RHS bareword condition &User-Name == 192.168.0.1 -match &User-Name == 192.168.0.1 +match &User-Name == 192.168.0.1 # Forcefully cast LHS bareword condition 192.168.0.1 == &User-Name -match &User-Name == 192.168.0.1 +match &User-Name == 192.168.0.1 # Forcefully cast RHS single quotes #condition &Framed-IP-Address == '192.168.0.1' diff --git a/src/tests/unit/condition/ip.txt b/src/tests/unit/condition/ip.txt index 4d664ba4cbe..7ca6df7a6aa 100644 --- a/src/tests/unit/condition/ip.txt +++ b/src/tests/unit/condition/ip.txt @@ -11,7 +11,7 @@ condition &Packet-Src-IPv6-Address == :: match &Packet-Src-IPv6-Address == :: condition &Packet-Src-IP-Address == * -match &Packet-Src-IP-Address == 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