From e24acae66e2f02d9c2ccffd55f431e37455e7dc8 Mon Sep 17 00:00:00 2001 From: mbkma Date: Wed, 18 Mar 2020 19:52:10 +0100 Subject: clear MPNumbers before return --- src/mp-trigonometric.c | 18 +++++++++++++++--- src/mp.c | 17 +++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/mp-trigonometric.c b/src/mp-trigonometric.c index ca19ca4..622d2dd 100644 --- a/src/mp-trigonometric.c +++ b/src/mp-trigonometric.c @@ -87,7 +87,7 @@ mp_sin(const MPNumber *x, MPAngleUnit unit, MPNumber *z) mp_set_from_mp(x, z); else convert_to_radians(x, unit, z); - mpc_sin(z->num, z->num, MPC_RNDNN); + mpc_sin(z->num, z->num, MPC_RNDNN); } @@ -98,7 +98,7 @@ mp_cos(const MPNumber *x, MPAngleUnit unit, MPNumber *z) mp_set_from_mp(x, z); else convert_to_radians(x, unit, z); - mpc_cos(z->num, z->num, MPC_RNDNN); + mpc_cos(z->num, z->num, MPC_RNDNN); } @@ -113,12 +113,15 @@ mp_tan(const MPNumber *x, MPAngleUnit unit, MPNumber *z) mp_get_pi(&pi); mp_divide_integer(&pi, 2, &t1); mp_subtract(&x_radians, &t1, &t1); - mp_divide(&t1, &pi, &t1); + mp_divide(&t1, &pi, &t1); if (mp_is_integer(&t1)) { /* Translators: Error displayed when tangent value is undefined */ mperr(_("Tangent is undefined for angles that are multiples of π (180°) from π∕2 (90°)")); mp_set_from_integer(0, z); + mp_clear(&x_radians); + mp_clear(&pi); + mp_clear(&t1); return; } @@ -146,6 +149,8 @@ mp_asin(const MPNumber *x, MPAngleUnit unit, MPNumber *z) /* Translators: Error displayed when inverse sine value is undefined */ mperr(_("Inverse sine is undefined for values outside [-1, 1]")); mp_set_from_integer(0, z); + mp_clear(&x_max); + mp_clear(&x_min); return; } mpc_asin(z->num, x->num, MPC_RNDNN); @@ -169,6 +174,8 @@ mp_acos(const MPNumber *x, MPAngleUnit unit, MPNumber *z) /* Translators: Error displayed when inverse sine value is undefined */ mperr(_("Inverse cosine is undefined for values outside [-1, 1]")); mp_set_from_integer(0, z); + mp_clear(&x_max); + mp_clear(&x_min); return; } mpc_acos(z->num, x->num, MPC_RNDNN); @@ -193,6 +200,8 @@ mp_atan(const MPNumber *x, MPAngleUnit unit, MPNumber *z) /* Translators: Error displayed when inverse sine value is undefined */ mperr(_("Arctangent function is undefined for values i and -i")); mp_set_from_integer(0, z); + mp_clear(&i); + mp_clear(&minus_i); return; } mpc_atan(z->num, x->num, MPC_RNDNN); @@ -243,6 +252,7 @@ mp_acosh(const MPNumber *x, MPNumber *z) /* Translators: Error displayed when inverse hyperbolic cosine value is undefined */ mperr(_("Inverse hyperbolic cosine is undefined for values less than one")); mp_set_from_integer(0, z); + mp_clear(&t); return; } @@ -264,6 +274,8 @@ mp_atanh(const MPNumber *x, MPNumber *z) /* Translators: Error displayed when inverse hyperbolic tangent value is undefined */ mperr(_("Inverse hyperbolic tangent is undefined for values outside (-1, 1)")); mp_set_from_integer(0, z); + mp_clear(&x_max); + mp_clear(&x_min); return; } mpc_atanh(z->num, x->num, MPC_RNDNN); diff --git a/src/mp.c b/src/mp.c index 1286f56..c66285e 100644 --- a/src/mp.c +++ b/src/mp.c @@ -622,27 +622,27 @@ mp_factorize(const MPNumber *x) { GList *list = NULL; MPNumber *factor = g_slice_alloc0(sizeof(MPNumber)); - MPNumber value = mp_new(); - MPNumber tmp = mp_new(); - MPNumber divisor = mp_new(); - MPNumber root = mp_new(); - MPNumber int_max = mp_new(); mpc_init2(factor->num, PRECISION); + MPNumber value = mp_new(); mp_abs(x, &value); if (mp_is_zero(&value)) { mp_set_from_mp(&value, factor); list = g_list_append(list, factor); + mp_clear(&value); return list; } + MPNumber tmp = mp_new(); mp_set_from_integer(1, &tmp); if (mp_is_equal(&value, &tmp)) { mp_set_from_mp(x, factor); list = g_list_append(list, factor); + mp_clear(&value); + mp_clear(&tmp); return list; } @@ -650,16 +650,20 @@ mp_factorize(const MPNumber *x) uint64_t num = 1; num = num << 63; num += (num - 1); + MPNumber int_max = mp_new(); mp_set_from_unsigned_integer(num, &int_max); if (mp_is_less_equal(x, &int_max)) { list = mp_factorize_unit64(mp_to_unsigned_integer(&value)); if (mp_is_negative(x)) mp_invert_sign(list->data, list->data); + mp_clear(&value); + mp_clear(&tmp); mp_clear(&int_max); return list; } + MPNumber divisor = mp_new(); mp_set_from_integer(2, &divisor); while (TRUE) { @@ -676,6 +680,7 @@ mp_factorize(const MPNumber *x) break; } + MPNumber root = mp_new(); mp_set_from_integer(3, &divisor); mp_sqrt(&value, &root); while (mp_is_less_equal(&divisor, &root)) @@ -723,9 +728,9 @@ mp_factorize_unit64(uint64_t n) { GList *list = NULL; MPNumber *factor = g_slice_alloc0(sizeof(MPNumber)); - MPNumber tmp = mp_new(); mpc_init2(factor->num, PRECISION); + MPNumber tmp = mp_new(); mp_set_from_unsigned_integer(2, &tmp); while (n % 2 == 0) { -- cgit v1.2.1