]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
create and use T_ADD, T_SUB, T_MUL, and T_DIV
authorAlan T. DeKok <aland@freeradius.org>
Fri, 19 Nov 2021 21:00:29 +0000 (16:00 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 19 Nov 2021 21:11:58 +0000 (16:11 -0500)
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.

src/bin/unit_test_attribute.c
src/lib/util/calc.c
src/lib/util/token.c
src/lib/util/token.h

index ca30dcef762228ba1bbb1b9739edc2295df82dff..c61af56319bb81e024931be4e73bba6a80c2f3fd 100644 (file)
@@ -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
index 3a846ceb2d858e090a86a18e849e9c0381abe153..aaacbba605cbe311940ff6e55219cbbc77198944 100644 (file)
@@ -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);
 
index 8b4a3693ebbedd8cd0f6c51bcfe80bb73fbd687f..c894721c2a21bae040242eabe66d8826f1bdf14e 100644 (file)
@@ -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] = "+=",
index cc883c2c21126218c3de5e40b1463f4fea4f4bf8..ad2f80e4e5f98b630db01b7617989a16a2fc9ec1 100644 (file)
@@ -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,                     /* ++ */
 
        /*