From cdd211d0a3f8bf977ecca67e72afbc63d53a72ee Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Tue, 26 Sep 2017 12:49:50 +0200 Subject: check for NaN in Fraction which can result from division by zero in earlier code, rather assert explicitly than suffer from weird very large sal_Int64 values (which is what NaN converts to, if we let it do the implicit conversion) Change-Id: Id059b84906bbc90a4fa51489ca96dc0267bb9342 Reviewed-on: https://gerrit.libreoffice.org/42798 Tested-by: Jenkins Reviewed-by: Noel Grandin --- tools/source/generic/fract.cxx | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/source/generic/fract.cxx b/tools/source/generic/fract.cxx index 7d89ae812e6f..1a78d4dfc654 100644 --- a/tools/source/generic/fract.cxx +++ b/tools/source/generic/fract.cxx @@ -86,6 +86,27 @@ Fraction::Fraction( sal_Int64 nNum, sal_Int64 nDen ) : mpImpl(new Impl) mpImpl->valid = true; } +/** + * only here to prevent passing of NaN + */ +Fraction::Fraction( double nNum, double nDen ) : mpImpl(new Impl) +{ + assert( !std::isnan(nNum) ); + assert( !std::isnan(nDen) ); + assert( nNum >= std::numeric_limits::min() ); + assert( nNum <= std::numeric_limits::max( )); + assert( nDen >= std::numeric_limits::min() ); + assert( nDen <= std::numeric_limits::max( )); + if ( nDen == 0 ) + { + mpImpl->valid = false; + SAL_WARN( "tools.fraction", "'Fraction(" << nNum << ",0)' invalid fraction created" ); + return; + } + mpImpl->value.assign( sal_Int64(nNum), sal_Int64(nDen)); + mpImpl->valid = true; +} + Fraction::Fraction( double dVal ) : mpImpl(new Impl) { try @@ -439,7 +460,8 @@ SvStream& WriteFraction( SvStream& rOStream, const Fraction& rFract ) static boost::rational rational_FromDouble(double dVal) { if ( dVal > std::numeric_limits::max() || - dVal < std::numeric_limits::min() ) + dVal < std::numeric_limits::min() || + std::isnan(dVal) ) throw boost::bad_rational(); const sal_Int32 nMAX = std::numeric_limits::max() / 10; -- cgit