From: Alan T. DeKok Date: Fri, 19 Nov 2021 21:00:29 +0000 (-0500) Subject: create and use T_ADD, T_SUB, T_MUL, and T_DIV X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5464d7a7884a9b29240ee8c8f8b2c5bdaba8574c;p=thirdparty%2Ffreeradius-server.git create and use T_ADD, T_SUB, T_MUL, and T_DIV note that we don't (yet) add them to fr_tokens_table[] That's because attribute names have "-" in them. Adding "-" as a token means that we no longer parse attribute names in the same way. The solution is likely to update the various pair parsers to avoid the 1999-era gettoken() APIs, and instead use more modern equivalents. For now, we just define and use add/sub/mul/div for the various calc tests. --- diff --git a/src/bin/unit_test_attribute.c b/src/bin/unit_test_attribute.c index ca30dcef762..c61af56319b 100644 --- a/src/bin/unit_test_attribute.c +++ b/src/bin/unit_test_attribute.c @@ -1168,10 +1168,12 @@ static size_t command_normalise_attribute(command_result_t *result, command_file } static const fr_token_t token2op[UINT8_MAX + 1] = { - [ '+' ] = T_OP_ADD_EQ, - [ '-' ] = T_OP_SUB_EQ, + [ '+' ] = T_ADD, + [ '-' ] = T_SUB, + [ '*' ] = T_MUL, + [ '/' ] = T_DIV, [ '^' ] = T_OP_PREPEND, - [ '.' ] = T_OP_ADD_EQ, + [ '.' ] = T_ADD, }; /** Perform calculations diff --git a/src/lib/util/calc.c b/src/lib/util/calc.c index 3a846ceb2d8..aaacbba605c 100644 --- a/src/lib/util/calc.c +++ b/src/lib/util/calc.c @@ -246,14 +246,14 @@ static int calc_date(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t } switch (op) { - case T_OP_ADD_EQ: + case T_ADD: if (!fr_add(&when, fr_time_delta_unwrap(a->vb_time_delta), fr_time_delta_unwrap(b->vb_time_delta))) return OVERFLOW; dst->vb_date = fr_unix_time_from_integer(&overflow, when, FR_TIME_RES_NSEC); if (overflow) return OVERFLOW; /* overflow */ break; - case T_OP_SUB_EQ: + case T_SUB: if (!fr_sub(&when, fr_time_delta_unwrap(a->vb_time_delta), fr_time_delta_unwrap(b->vb_time_delta))) return OVERFLOW; dst->vb_date = fr_unix_time_from_integer(&overflow, when, FR_TIME_RES_NSEC); @@ -279,7 +279,7 @@ static int calc_time_delta(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value * cannot add two dates to get a time delta. */ if ((a->type == FR_TYPE_DATE) && (b->type == FR_TYPE_DATE)) { - if (op != T_OP_SUB_EQ) { + if (op != T_SUB) { fr_strerror_const("Cannot perform operation on two dates"); return -1; } @@ -313,12 +313,12 @@ static int calc_time_delta(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value } switch (op) { - case T_OP_ADD_EQ: + case T_ADD: if (!fr_add(&when, fr_time_delta_unwrap(a->vb_time_delta), fr_time_delta_unwrap(b->vb_time_delta))) return OVERFLOW; dst->vb_time_delta = fr_time_delta_wrap(when); break; - case T_OP_SUB_EQ: + case T_SUB: if (!fr_sub(&when, fr_time_delta_unwrap(a->vb_time_delta), fr_time_delta_unwrap(b->vb_time_delta))) return OVERFLOW; dst->vb_time_delta = fr_time_delta_wrap(when); break; @@ -367,7 +367,7 @@ static int calc_octets(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons fr_value_box_memdup_shallow(dst, dst->enumv, buf, len, a->tainted | b->tainted); break; - case T_OP_ADD_EQ: /* dst = a . b */ + case T_ADD: /* dst = a . b */ buf = talloc_array(ctx, uint8_t, len); if (!buf) goto oom; @@ -425,7 +425,7 @@ static int calc_string(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t cons fr_value_box_strdup_shallow(dst, dst->enumv, buf, a->tainted | b->tainted); break; - case T_OP_ADD_EQ: + case T_ADD: buf = talloc_array(ctx, char, len + 1); if (!buf) goto oom; @@ -510,7 +510,7 @@ static int calc_ipv4_addr(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_ b = &two; switch (op) { - case T_OP_ADD_EQ: + case T_ADD: /* * For simplicity, make sure that the prefix is first. */ @@ -609,7 +609,7 @@ static int calc_ipv6_addr(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_ b = &two; switch (op) { - case T_OP_ADD_EQ: + case T_ADD: /* * For simplicity, make sure that the prefix is first. */ @@ -674,11 +674,11 @@ static int calc_float32(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_bo } switch (op) { - case T_OP_ADD_EQ: + case T_ADD: dst->vb_float32 = a->vb_float64 + b->vb_float64; break; - case T_OP_SUB_EQ: + case T_SUB: dst->vb_float32 = a->vb_float64 - b->vb_float64; break; @@ -709,11 +709,11 @@ static int calc_float64(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_bo } switch (op) { - case T_OP_ADD_EQ: + case T_ADD: dst->vb_float64 = a->vb_float64 + b->vb_float64; break; - case T_OP_SUB_EQ: + case T_SUB: dst->vb_float64 = a->vb_float64 - b->vb_float64; break; @@ -728,11 +728,11 @@ static int calc_float64(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_bo #define CALC(_t) static int calc_ ## _t(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t const *in1, fr_token_t op, fr_value_box_t const *in2) \ { \ switch (op) { \ - case T_OP_ADD_EQ: \ + case T_ADD: \ if (!fr_add(&dst->vb_ ## _t, in1->vb_ ## _t, in2->vb_ ## _t)) return OVERFLOW; \ break; \ \ - case T_OP_SUB_EQ: \ + case T_SUB: \ if (!fr_sub(&dst->vb_ ## _t, in1->vb_ ## _t, in2->vb_ ## _t)) return OVERFLOW; \ break; \ \ @@ -939,8 +939,8 @@ int fr_value_calc(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t hint, fr_value hint = FR_TYPE_BOOL; break; - case T_OP_ADD_EQ: - case T_OP_SUB_EQ: + case T_ADD: + case T_SUB: if (a->type == b->type) { hint = a->type; break; @@ -1035,8 +1035,8 @@ int fr_value_calc(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t hint, fr_value rcode = 0; break; - case T_OP_ADD_EQ: - case T_OP_SUB_EQ: + case T_ADD: + case T_SUB: case T_OP_PREPEND: fr_assert(hint != FR_TYPE_NULL); diff --git a/src/lib/util/token.c b/src/lib/util/token.c index 8b4a3693ebb..c894721c2a2 100644 --- a/src/lib/util/token.c +++ b/src/lib/util/token.c @@ -80,6 +80,11 @@ char const *fr_tokens[T_TOKEN_LAST] = { [T_COMMA] = ",", [T_SEMICOLON] = ";", + [T_ADD] = "+", + [T_SUB] = "-", + [T_MUL] = "*", + [T_DIV] = "/", + [T_OP_INCRM] = "++", [T_OP_ADD_EQ] = "+=", diff --git a/src/lib/util/token.h b/src/lib/util/token.h index cc883c2c211..ad2f80e4e5f 100644 --- a/src/lib/util/token.h +++ b/src/lib/util/token.h @@ -45,6 +45,14 @@ typedef enum fr_token { T_COMMA, /* , */ T_SEMICOLON, /* ; */ + T_ADD, /* + */ + T_SUB, /* - */ + T_MUL, /* * */ + T_DIV, /* / */ + + /* + * Only used by LDAP ??? + */ T_OP_INCRM, /* ++ */ /*