From: Alan T. DeKok Date: Wed, 15 Dec 2021 20:47:26 +0000 (-0500) Subject: clarify and extend error messages X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ed34e34814f3182f1e2133221333d3cb4816d937;p=thirdparty%2Ffreeradius-server.git clarify and extend error messages --- diff --git a/src/lib/util/calc.c b/src/lib/util/calc.c index ccb9b04a5f2..cef11a4c25d 100644 --- a/src/lib/util/calc.c +++ b/src/lib/util/calc.c @@ -31,6 +31,8 @@ RCSID("$Id$") #define swap(_a, _b) do { __typeof__ (_a) _tmp = _a; _a = _b; _b = _tmp; } while (0) +#define ERR_ZERO (-5) +#define ERR_UNDERFLOW (-4) #define ERR_OVERFLOW (-3) #define ERR_INVALID (-2) @@ -227,12 +229,19 @@ static int invalid_type(fr_type_t type) static int handle_result(fr_type_t type, fr_token_t op, int rcode) { - if (rcode == ERR_OVERFLOW) { - fr_strerror_printf("Value overflows/underflows when calculating answer for %s", + if (rcode == ERR_ZERO) { + fr_strerror_const("Cannot divide by zero."); + + } else if (rcode == ERR_UNDERFLOW) { + fr_strerror_printf("Value underflows '%s' when calculating result.", + fr_table_str_by_value(fr_value_box_type_table, type, "")); + + } else if (rcode == ERR_OVERFLOW) { + fr_strerror_printf("Value overflows '%s' when calculating result.", fr_table_str_by_value(fr_value_box_type_table, type, "")); } else if (rcode == ERR_INVALID) { - fr_strerror_printf("Invalid assignment operator %s for destination type %s", + fr_strerror_printf("Invalid assignment operator '%s' for result type '%s'.", fr_tokens[op], fr_table_str_by_value(fr_value_box_type_table, type, "")); } @@ -270,7 +279,7 @@ static int calc_bool(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t /* * 0-1 = -1, which isn't a valid boolean value. */ - if (a->vb_bool < b->vb_bool) return ERR_OVERFLOW; + if (a->vb_bool < b->vb_bool) return ERR_UNDERFLOW; dst->vb_bool = a->vb_bool - b->vb_bool; break; @@ -324,14 +333,14 @@ static int calc_date(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t if (!fr_add(&when, fr_time_delta_unwrap(a->vb_time_delta), fr_time_delta_unwrap(b->vb_time_delta))) return ERR_OVERFLOW; dst->vb_date = fr_unix_time_from_integer(&overflow, when, FR_TIME_RES_NSEC); - if (overflow) return ERR_OVERFLOW; /* overflow */ + if (overflow) return ERR_OVERFLOW; break; case T_SUB: - if (!fr_sub(&when, fr_time_delta_unwrap(a->vb_time_delta), fr_time_delta_unwrap(b->vb_time_delta))) return ERR_OVERFLOW; + if (!fr_sub(&when, fr_time_delta_unwrap(a->vb_time_delta), fr_time_delta_unwrap(b->vb_time_delta))) return ERR_UNDERFLOW; dst->vb_date = fr_unix_time_from_integer(&overflow, when, FR_TIME_RES_NSEC); - if (overflow) return ERR_OVERFLOW; /* overflow */ + if (overflow) return ERR_UNDERFLOW; break; default: @@ -382,7 +391,7 @@ static int calc_time_delta(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value break; case T_SUB: - if (!fr_sub(&when, fr_time_delta_unwrap(a->vb_time_delta), fr_time_delta_unwrap(b->vb_time_delta))) return ERR_OVERFLOW; + if (!fr_sub(&when, fr_time_delta_unwrap(a->vb_time_delta), fr_time_delta_unwrap(b->vb_time_delta))) return ERR_UNDERFLOW; dst->vb_time_delta = fr_time_delta_wrap(when); break; @@ -839,7 +848,7 @@ static int calc_float32(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_bo break; case T_DIV: - if (fpclassify(b->vb_float64) == FP_ZERO) return ERR_OVERFLOW; + if (fpclassify(b->vb_float64) == FP_ZERO) return ERR_ZERO; dst->vb_float32 = a->vb_float64 / b->vb_float64; break; @@ -884,7 +893,7 @@ static int calc_float64(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_bo break; case T_DIV: - if (fpclassify(b->vb_float64) == FP_ZERO) return ERR_OVERFLOW; + if (fpclassify(b->vb_float64) == FP_ZERO) return ERR_ZERO; dst->vb_float64 = a->vb_float64 / b->vb_float64; break; @@ -905,7 +914,7 @@ static int calc_float64(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_bo break; \ \ case T_SUB: \ - if (!fr_sub(&dst->vb_ ## _t, in1->vb_ ## _t, in2->vb_ ## _t)) return ERR_OVERFLOW; \ + if (!fr_sub(&dst->vb_ ## _t, in1->vb_ ## _t, in2->vb_ ## _t)) return ERR_UNDERFLOW; \ break; \ \ case T_MUL: \ diff --git a/src/tests/unit/calc.txt b/src/tests/unit/calc.txt index 330e75a120e..b9a44d3f68a 100644 --- a/src/tests/unit/calc.txt +++ b/src/tests/unit/calc.txt @@ -7,7 +7,7 @@ # integers # calc uint8 255 + uint8 255 -> uint8 -match Value overflows/underflows when calculating answer for uint8 +match Value overflows 'uint8' when calculating result. calc uint8 127 + uint8 127 -> uint8 match 254 @@ -27,10 +27,10 @@ calc int8 0x7f & uint8 0x40 -> uint8 match 64 calc uint8 5 - uint8 16 -> uint8 -match Value overflows/underflows when calculating answer for uint8 +match Value underflows 'uint8' when calculating result. calc uint32 5 - uint32 16 -> uint32 -match Value overflows/underflows when calculating answer for uint32 +match Value underflows 'uint32' when calculating result. # # Intermediate values are too large for destination, but the