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/sbxuint.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/sbxuint.cxx')
-rw-r--r-- | basic/source/sbx/sbxuint.cxx | 32 |
1 files changed, 3 insertions, 29 deletions
diff --git a/basic/source/sbx/sbxuint.cxx b/basic/source/sbx/sbxuint.cxx index a3751e2661a4..d8eeec4bc94d 100644 --- a/basic/source/sbx/sbxuint.cxx +++ b/basic/source/sbx/sbxuint.cxx @@ -107,16 +107,7 @@ start: nRes = static_cast<sal_uInt16>(p->uInt64); break; case SbxSINGLE: - if( !o3tl::convertsToAtMost(o3tl::roundAway(p->nSingle), SbxMAXUINT) ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXUINT; - } - else if( p->nSingle < 0 ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0; - } - else - nRes = static_cast<sal_uInt16>( p->nSingle + 0.5 ); + nRes = ImpDoubleToUShort(p->nSingle); break; case SbxDATE: case SbxDOUBLE: @@ -133,16 +124,7 @@ start: else dVal = p->nDouble; - if( !o3tl::convertsToAtMost(o3tl::roundAway(dVal), SbxMAXUINT) ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXUINT; - } - else if( dVal < 0 ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0; - } - else - nRes = static_cast<sal_uInt16>( dVal + 0.5 ); + nRes = ImpDoubleToUShort(dVal); break; } case SbxBYREF | SbxSTRING: @@ -156,16 +138,8 @@ start: SbxDataType t; if( ImpScan( *p->pOUString, d, t, nullptr, true ) != ERRCODE_NONE ) nRes = 0; - else if( !o3tl::convertsToAtMost(o3tl::roundAway(d), SbxMAXUINT) ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXUINT; - } - else if( d < 0 ) - { - SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0; - } else - nRes = static_cast<sal_uInt16>( d + 0.5 ); + nRes = ImpDoubleToUShort(d); } break; case SbxOBJECT: |