diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-09-26 12:49:50 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-09-27 09:05:55 +0200 |
commit | cdd211d0a3f8bf977ecca67e72afbc63d53a72ee (patch) | |
tree | e0c5003ac5de84695efd86babfedb248af74df35 /tools | |
parent | 467724410dc470ec259131f97abd836fe9b021a1 (diff) |
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 <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/source/generic/fract.cxx | 24 |
1 files changed, 23 insertions, 1 deletions
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<sal_Int32>::min() ); + assert( nNum <= std::numeric_limits<sal_Int32>::max( )); + assert( nDen >= std::numeric_limits<sal_Int32>::min() ); + assert( nDen <= std::numeric_limits<sal_Int32>::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<sal_Int32> rational_FromDouble(double dVal) { if ( dVal > std::numeric_limits<sal_Int32>::max() || - dVal < std::numeric_limits<sal_Int32>::min() ) + dVal < std::numeric_limits<sal_Int32>::min() || + std::isnan(dVal) ) throw boost::bad_rational(); const sal_Int32 nMAX = std::numeric_limits<sal_Int32>::max() / 10; |