diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-04-08 23:52:01 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-04-08 23:52:01 +0200 |
commit | 84a40be0bce66cda800de7891f758a0c69afc7fa (patch) | |
tree | aff8ee61032353024cf1a8429f0804162f81085a /src/util | |
parent | 1554cc310d450ee5cfbf3afd947ed8a063043254 (diff) |
fix #6170 and rest of #6164
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/amount.c | 39 | ||||
-rw-r--r-- | src/util/test_amount.c | 14 |
2 files changed, 26 insertions, 27 deletions
diff --git a/src/util/amount.c b/src/util/amount.c index f96ab9c49..2de937620 100644 --- a/src/util/amount.c +++ b/src/util/amount.c @@ -394,12 +394,9 @@ TALER_amount_cmp (const struct TALER_Amount *a1, * @param[out] diff where to store (@a a1 - @a a2), or invalid if @a a2 > @a a1 * @param a1 amount to subtract from * @param a2 amount to subtract - * @return #GNUNET_OK if the subtraction worked, - * #GNUNET_NO if @a a1 = @a a2 - * #GNUNET_SYSERR if @a a2 > @a a1 or currencies are incompatible; - * @a diff is set to invalid + * @return operation status, negative on failures */ -int +enum TALER_AmountArithmeticResult TALER_amount_subtract (struct TALER_Amount *diff, const struct TALER_Amount *a1, const struct TALER_Amount *a2) @@ -412,7 +409,7 @@ TALER_amount_subtract (struct TALER_Amount *diff, a2)) { invalidate (diff); - return GNUNET_SYSERR; + return TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE; } /* make local copies to avoid aliasing problems between diff and a1/a2 */ @@ -422,7 +419,7 @@ TALER_amount_subtract (struct TALER_Amount *diff, (GNUNET_SYSERR == TALER_amount_normalize (&n2)) ) { invalidate (diff); - return GNUNET_SYSERR; + return TALER_AAR_INVALID_NORMALIZATION_FAILED; } if (n1.fraction < n2.fraction) @@ -430,7 +427,7 @@ TALER_amount_subtract (struct TALER_Amount *diff, if (0 == n1.value) { invalidate (diff); - return GNUNET_SYSERR; + return TALER_AAR_INVALID_NEGATIVE_RESULT; } n1.fraction += TALER_AMOUNT_FRAC_BASE; n1.value--; @@ -438,7 +435,7 @@ TALER_amount_subtract (struct TALER_Amount *diff, if (n1.value < n2.value) { invalidate (diff); - return GNUNET_SYSERR; + return TALER_AAR_INVALID_NEGATIVE_RESULT; } GNUNET_assert (GNUNET_OK == TALER_amount_get_zero (n1.currency, @@ -449,8 +446,8 @@ TALER_amount_subtract (struct TALER_Amount *diff, diff->value = n1.value - n2.value; if ( (0 == diff->fraction) && (0 == diff->value) ) - return GNUNET_NO; - return GNUNET_OK; + return TALER_AAR_RESULT_ZERO; + return TALER_AAR_RESULT_POSITIVE; } @@ -460,10 +457,9 @@ TALER_amount_subtract (struct TALER_Amount *diff, * @param[out] sum where to store @a a1 + @a a2, set to "invalid" on overflow * @param a1 first amount to add * @param a2 second amount to add - * @return #GNUNET_OK if the addition worked, - * #GNUNET_SYSERR on overflow + * @return operation status, negative on failures */ -int +enum TALER_AmountArithmeticResult TALER_amount_add (struct TALER_Amount *sum, const struct TALER_Amount *a1, const struct TALER_Amount *a2) @@ -476,7 +472,7 @@ TALER_amount_add (struct TALER_Amount *sum, TALER_amount_cmp_currency (a1, a2)) { invalidate (sum); - return GNUNET_SYSERR; + return TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE; } /* make local copies to avoid aliasing problems between diff and a1/a2 */ @@ -486,7 +482,7 @@ TALER_amount_add (struct TALER_Amount *sum, (GNUNET_SYSERR == TALER_amount_normalize (&n2)) ) { invalidate (sum); - return GNUNET_SYSERR; + return TALER_AAR_INVALID_NORMALIZATION_FAILED; } GNUNET_assert (GNUNET_OK == @@ -497,13 +493,13 @@ TALER_amount_add (struct TALER_Amount *sum, { /* integer overflow */ invalidate (sum); - return GNUNET_SYSERR; + return TALER_AAR_INVALID_RESULT_OVERFLOW; } if (res.value > MAX_AMOUNT_VALUE) { /* too large to be legal */ invalidate (sum); - return GNUNET_SYSERR; + return TALER_AAR_INVALID_RESULT_OVERFLOW; } res.fraction = n1.fraction + n2.fraction; if (GNUNET_SYSERR == @@ -511,10 +507,13 @@ TALER_amount_add (struct TALER_Amount *sum, { /* integer overflow via carry from fraction */ invalidate (sum); - return GNUNET_SYSERR; + return TALER_AAR_INVALID_RESULT_OVERFLOW; } *sum = res; - return GNUNET_OK; + if ( (0 == sum->fraction) && + (0 == sum->value) ) + return TALER_AAR_RESULT_ZERO; + return TALER_AAR_RESULT_POSITIVE; } diff --git a/src/util/test_amount.c b/src/util/test_amount.c index 03f15cb88..8a83e4cf6 100644 --- a/src/util/test_amount.c +++ b/src/util/test_amount.c @@ -117,7 +117,7 @@ main (int argc, &a2)); /* test subtraction failure (currency mismatch) */ - GNUNET_assert (GNUNET_SYSERR == + GNUNET_assert (TALER_AAR_INVALID_CURRENCIES_INCOMPATIBLE == TALER_amount_subtract (&a3, &a3, &a2)); @@ -125,7 +125,7 @@ main (int argc, TALER_amount_normalize (&a3)); /* test subtraction failure (negative result) */ - GNUNET_assert (GNUNET_SYSERR == + GNUNET_assert (TALER_AAR_INVALID_NEGATIVE_RESULT == TALER_amount_subtract (&a3, &a1, &a2)); @@ -133,11 +133,11 @@ main (int argc, TALER_amount_normalize (&a3)); /* test subtraction success cases */ - GNUNET_assert (GNUNET_YES == + GNUNET_assert (TALER_AAR_RESULT_POSITIVE == TALER_amount_subtract (&a3, &a2, &a1)); - GNUNET_assert (GNUNET_NO == + GNUNET_assert (TALER_AAR_RESULT_ZERO == TALER_amount_subtract (&a3, &a1, &a1)); @@ -147,7 +147,7 @@ main (int argc, TALER_amount_normalize (&a3)); /* test addition success */ - GNUNET_assert (GNUNET_OK == + GNUNET_assert (TALER_AAR_RESULT_POSITIVE == TALER_amount_add (&a3, &a3, &a2)); @@ -189,7 +189,7 @@ main (int argc, a1.value = UINT64_MAX - 5; a2.fraction = 2; a2.value = 5; - GNUNET_assert (GNUNET_SYSERR == + GNUNET_assert (TALER_AAR_INVALID_RESULT_OVERFLOW == TALER_amount_add (&a3, &a1, &a2)); /* test addition with underflow on fraction */ @@ -197,7 +197,7 @@ main (int argc, a1.value = UINT64_MAX; a2.fraction = 2; a2.value = 0; - GNUNET_assert (GNUNET_OK == + GNUNET_assert (TALER_AAR_RESULT_POSITIVE == TALER_amount_subtract (&a3, &a1, &a2)); GNUNET_assert (UINT64_MAX - 1 == a3.value); GNUNET_assert (TALER_AMOUNT_FRAC_BASE - 1 == a3.fraction); |