diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2021-11-24 13:43:05 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2021-12-01 14:03:30 +0100 |
commit | c28a13b9e2bd864e8c574d604bcd6da9e93fb476 (patch) | |
tree | c00f9e065e8f62e755bfbf242ff67eb6ef202165 /basic/source/sbx/sbxlng.cxx | |
parent | 25a368c30acb54e0819d2b2b890a3fd1530d8a76 (diff) |
Unify and simplify floating-point-to-integer conversion
* Round the number once, to avoid doing it three times for a successful conversion.
* Round to nearest before convertsToAtMost/convertsToAtLeast, to handle cases like
Dim n As Integer
n = 32767.4
which should succeed.
* Add overflow checks to Hyper (U/Int64) types.
Change-Id: Ib10837e6df3cc1e3aba7a657e882bd40e344fd3b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/126173
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'basic/source/sbx/sbxlng.cxx')
-rw-r--r-- | basic/source/sbx/sbxlng.cxx | 32 |
1 files changed, 3 insertions, 29 deletions
diff --git a/basic/source/sbx/sbxlng.cxx b/basic/source/sbx/sbxlng.cxx index bda401b981db..915cf861025f 100644 --- a/basic/source/sbx/sbxlng.cxx +++ b/basic/source/sbx/sbxlng.cxx @@ -59,16 +59,7 @@ start: nRes = static_cast<sal_Int32>(p->nULong); break; case SbxSINGLE: - if( !o3tl::convertsToAtMost(o3tl::roundAway(p->nSingle), SbxMAXLNG) ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXLNG; - } - else if( !o3tl::convertsToAtLeast(o3tl::roundAway(p->nSingle), SbxMINLNG) ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMINLNG; - } - else - nRes = static_cast<sal_Int32>(rtl::math::round( p->nSingle )); + nRes = ImpDoubleToLong(p->nSingle); break; case SbxSALINT64: nRes = p->nInt64; @@ -100,16 +91,7 @@ start: else dVal = p->nDouble; - if( !o3tl::convertsToAtMost(o3tl::roundAway(dVal), SbxMAXLNG) ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXLNG; - } - else if( !o3tl::convertsToAtLeast(o3tl::roundAway(dVal), SbxMINLNG) ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMINLNG; - } - else - nRes = static_cast<sal_Int32>(rtl::math::round( dVal )); + nRes = ImpDoubleToLong(dVal); break; } case SbxBYREF | SbxSTRING: @@ -123,16 +105,8 @@ start: SbxDataType t; if( ImpScan( *p->pOUString, d, t, nullptr, true ) != ERRCODE_NONE ) nRes = 0; - else if( !o3tl::convertsToAtMost(o3tl::roundAway(d), SbxMAXLNG) ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXLNG; - } - else if( !o3tl::convertsToAtLeast(o3tl::roundAway(d), SbxMINLNG) ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMINLNG; - } else - nRes = static_cast<sal_Int32>(rtl::math::round( d )); + nRes = ImpDoubleToLong(d); } break; case SbxOBJECT: |