]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add function to manage assignment operations
authorAlan T. DeKok <aland@freeradius.org>
Sat, 20 Nov 2021 14:13:15 +0000 (09:13 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 20 Nov 2021 14:19:06 +0000 (09:19 -0500)
src/lib/util/calc.c
src/lib/util/calc.h

index 58db2af2a0bb28886ebdfe787de3b0c5052c3792..4188b7f36bb22ff5d423202c1a264a5d19680777 100644 (file)
@@ -1113,3 +1113,52 @@ done:
 
        return rcode;
 }
+
+int fr_value_calc_assignment_op(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_token_t op, fr_value_box_t const *src)
+{
+       int rcode = -1;
+
+       if (!fr_type_is_leaf(dst->type)) return -1;
+
+       if (!fr_assignment_op[op]) goto invalid;
+
+       switch (op) {
+       case T_OP_EQ:
+       case T_OP_SET:
+               fr_value_box_clear(dst);
+               fr_value_box_copy(ctx, dst, src);
+               rcode = 0;
+               break;
+
+       case T_OP_ADD_EQ:
+       case T_OP_SUB_EQ:
+       case T_OP_PREPEND:
+               /*
+                *      Just call the binary op function.  It already
+                *      ensures that (a) the inputs are "const", and
+                *      (b) the output is over-written only as the
+                *      final step
+                */
+               rcode = fr_value_calc_binary_op(ctx, dst, dst->type, dst, op, src);
+               break;
+
+       default:
+       invalid:
+               rcode = ERR_INVALID;
+               break;
+       }
+
+       if (rcode == ERR_OVERFLOW) {
+               fr_strerror_printf("Value overflows/underflows when calculating answer for %s",
+                                  fr_table_str_by_value(fr_value_box_type_table, dst->type, "<INVALID>"));
+
+       } else if (rcode == ERR_INVALID) {
+               fr_strerror_printf("Invalid operator %s for destination type %s",
+                                  fr_tokens[op],
+                                  fr_table_str_by_value(fr_value_box_type_table, dst->type, "<INVALID>"));
+       }
+
+       if (rcode == 0) dst->tainted |= src->tainted;
+
+       return rcode;
+}
index b672901e64800836462fec73f77d99b17802d4e7..cfa90d2145203610cb455a76d4c1b430f08211d8 100644 (file)
@@ -31,7 +31,9 @@ RCSIDH(calc_h, "$Id$")
 extern "C" {
 #endif
 
-int fr_value_calc_binary_op(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t hint, fr_value_box_t const *a, fr_token_t op, fr_value_box_t const *b);
+int fr_value_calc_binary_op(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t hint, fr_value_box_t const *a, fr_token_t op, fr_value_box_t const *b) CC_HINT(nonnull(2,4,6));
+
+int fr_value_calc_assignment_op(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_token_t op, fr_value_box_t const *src) CC_HINT(nonnull(2,4));
 
 #ifdef __cplusplus
 }