]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add <<= and >>= along with tests
authorAlan T. DeKok <aland@freeradius.org>
Tue, 20 Feb 2024 14:55:02 +0000 (09:55 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 20 Feb 2024 15:15:27 +0000 (10:15 -0500)
the underlying calc.c code supported it, but it wasn't exposed
(or tested) through unlang

src/lib/server/cf_file.c
src/lib/server/map.c
src/lib/util/token.c
src/tests/keywords/edit-shift [new file with mode: 0644]

index 254acbec35fb8026e65acf8bd6f3a7f75cb17bd1..51d54f65af2e25c1aca5cafe83b6238b1ac5eaef 100644 (file)
@@ -2418,10 +2418,10 @@ check_for_eol:
        case T_OP_AND_EQ:
        case T_OP_OR_EQ:
        case T_OP_NE:
-       case T_RSHIFT:
+       case T_OP_RSHIFT_EQ:
        case T_OP_GE:
        case T_OP_GT:
-       case T_LSHIFT:
+       case T_OP_LSHIFT_EQ:
        case T_OP_LE:
        case T_OP_LT:
        case T_OP_CMP_EQ:
index 531ecad85122cb8670670bf1448075ddd51cbd96..bcf451c5a9cb755d450e75fe2c14ae50c4b8a765 100644 (file)
@@ -197,7 +197,11 @@ int map_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp,
                }
                rhs_rules = &my_rhs_rules;
 
-               if (edit) my_rhs_rules.enumv = tmpl_attr_tail_da(map->lhs);
+               if (edit) {
+                       if ((map->op != T_OP_RSHIFT_EQ) && (map->op != T_OP_LSHIFT_EQ)) {
+                               my_rhs_rules.enumv = tmpl_attr_tail_da(map->lhs);
+                       }
+               }
                break;
        }
 
@@ -293,9 +297,17 @@ int map_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp,
         *      If we know that the assignment is forbidden, then fail early.
         */
        if (tmpl_is_attr(map->lhs) && tmpl_is_data(map->rhs)) {
-               da = tmpl_attr_tail_da(map->lhs);
+               fr_type_t cast_type;
+
+               if ((map->op != T_OP_RSHIFT_EQ) && (map->op != T_OP_LSHIFT_EQ)) {
+                       da = tmpl_attr_tail_da(map->lhs);
+                       cast_type = da->type;
+               } else {
+                       da = NULL;
+                       cast_type = FR_TYPE_UINT32;
+               }
 
-               if (tmpl_cast_in_place(map->rhs, da->type, da) < 0) {
+               if (tmpl_cast_in_place(map->rhs, cast_type, da) < 0) {
                        cf_log_err(cp, "Invalid assignment - %s", fr_strerror());
                        goto error;
                }
index fb58cb9b0e6e0748cb53e8036a013801397d7c22..c7fa61e9893dbec45e3d4730b865f6d1507c0785 100644 (file)
@@ -52,10 +52,10 @@ fr_table_num_ordered_t const fr_tokens_table[] = {
        { L("="),       T_OP_EQ         },
        { L("!="),      T_OP_NE         },
        { L("!=="),     T_OP_CMP_NE_TYPE },
-       { L(">>="),     T_RSHIFT        },
+       { L(">>="),     T_OP_RSHIFT_EQ  },
        { L(">="),      T_OP_GE         },
        { L(">"),       T_OP_GT         },
-       { L("<<="),     T_LSHIFT        },
+       { L("<<="),     T_OP_LSHIFT_EQ  },
        { L("<="),      T_OP_LE         },
        { L("<"),       T_OP_LT         },
        { L("#"),       T_HASH          },
diff --git a/src/tests/keywords/edit-shift b/src/tests/keywords/edit-shift
new file mode 100644 (file)
index 0000000..926b450
--- /dev/null
@@ -0,0 +1,23 @@
+octets foo
+
+#
+#  left shift octets
+#
+&foo := 0xabcdef
+&foo <<= 2
+
+if (&foo != 0xef) {
+       test_fail
+}
+
+#
+#  And right shift
+#
+&foo := 0xabcdef
+
+
+&foo >>= 2
+if (&foo != 0xab) {
+       test_fail
+}
+success