From f2d62b11cd7d47925fd098b3947726313d6b296e Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Thu, 3 Feb 2022 09:43:32 +0000 Subject: cid#1497934 silence Division or modulo by zero MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I2f894e46b962adfc9a5c97dc6731d292a61939a4 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129405 Tested-by: Jenkins Reviewed-by: Caolán McNamara --- include/o3tl/unit_conversion.hxx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/o3tl/unit_conversion.hxx b/include/o3tl/unit_conversion.hxx index c1961ca2feb8..7f67882f7e3d 100644 --- a/include/o3tl/unit_conversion.hxx +++ b/include/o3tl/unit_conversion.hxx @@ -107,14 +107,21 @@ constexpr sal_Int64 MulDivSaturate(I n, sal_Int64 m, sal_Int64 d) return MulDiv(n, m, d); } +template constexpr std::common_type_t asserting_gcd(M m, N n) +{ + auto ret = std::gcd(m, n); + assert(ret != 0); + return ret; +} + // Packs integral multiplier and divisor for conversion from one unit to another struct m_and_d { sal_Int64 m; // multiplier sal_Int64 d; // divisor constexpr m_and_d(sal_Int64 _m, sal_Int64 _d) - : m(_m / std::gcd(_m, _d)) // make sure to use smallest quotients here because - , d(_d / std::gcd(_m, _d)) // they will be multiplied when building final table + : m(_m / asserting_gcd(_m, _d)) // make sure to use smallest quotients here because + , d(_d / asserting_gcd(_m, _d)) // they will be multiplied when building final table { assert(_m > 0 && _d > 0); } @@ -134,8 +141,7 @@ template constexpr auto prepareMDArray(const m_and_d (&mdBase)[N]) assert(mdBase[i].m < SAL_MAX_INT64 / mdBase[j].d); assert(mdBase[i].d < SAL_MAX_INT64 / mdBase[j].m); const sal_Int64 m = mdBase[i].m * mdBase[j].d, d = mdBase[i].d * mdBase[j].m; - const sal_Int64 g = std::gcd(m, d); - assert(g != 0); + const sal_Int64 g = asserting_gcd(m, d); a[i][j] = m / g; a[j][i] = d / g; } -- cgit