From c28a13b9e2bd864e8c574d604bcd6da9e93fb476 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Wed, 24 Nov 2021 13:43:05 +0300 Subject: 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 --- basic/source/sbx/sbxuint.cxx | 32 +++----------------------------- 1 file changed, 3 insertions(+), 29 deletions(-) (limited to 'basic/source/sbx/sbxuint.cxx') 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(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( 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( 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( d + 0.5 ); + nRes = ImpDoubleToUShort(d); } break; case SbxOBJECT: -- cgit