basic/source/sbx/sbxconv.hxx |   37 ++++++++++++++-
 basic/source/sbx/sbxdate.cxx |   60 ++----------------------
 basic/source/sbx/sbxdbl.cxx  |   60 ++----------------------
 basic/source/sbx/sbxint.cxx  |  105 ++++---------------------------------------
 basic/source/sbx/sbxlng.cxx  |   32 +------------
 basic/source/sbx/sbxsng.cxx  |   78 +++----------------------------
 basic/source/sbx/sbxuint.cxx |   32 +------------
 basic/source/sbx/sbxulng.cxx |   32 +------------
 8 files changed, 76 insertions(+), 360 deletions(-)

New commits:
commit c28a13b9e2bd864e8c574d604bcd6da9e93fb476
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Nov 24 13:43:05 2021 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Dec 1 14:03:30 2021 +0100

    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.kagan...@collabora.com>

diff --git a/basic/source/sbx/sbxconv.hxx b/basic/source/sbx/sbxconv.hxx
index dede96942297..a3837d9a3dbf 100644
--- a/basic/source/sbx/sbxconv.hxx
+++ b/basic/source/sbx/sbxconv.hxx
@@ -20,10 +20,45 @@
 #pragma once
 
 #include "sbxdec.hxx"
+#include <basic/sberrors.hxx>
 #include <basic/sbx.hxx>
+#include <basic/sbxcore.hxx>
+#include <basic/sbxdef.hxx>
+
+#include <o3tl/float_int_conversion.hxx>
+#include <rtl/math.hxx>
+#include <sal/types.h>
 
 class SbxArray;
 
+template <typename I> inline I DoubleTo(double f, I min, I max)
+{
+    f = rtl::math::round(f);
+    if (!o3tl::convertsToAtMost(f, max))
+    {
+        SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
+        return max;
+    }
+    if (!o3tl::convertsToAtLeast(f, min))
+    {
+        SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
+        return min;
+    }
+    return f;
+}
+
+inline auto ImpDoubleToChar(double f) { return DoubleTo<sal_Unicode>(f, 
SbxMINCHAR, SbxMAXCHAR); }
+inline auto ImpDoubleToByte(double f) { return DoubleTo<sal_uInt8>(f, 0, 
SbxMAXBYTE); }
+inline auto ImpDoubleToUShort(double f) { return DoubleTo<sal_uInt16>(f, 0, 
SbxMAXUINT); }
+inline auto ImpDoubleToInteger(double f) { return DoubleTo<sal_Int16>(f, 
SbxMININT, SbxMAXINT); }
+inline auto ImpDoubleToULong(double f) { return DoubleTo<sal_uInt32>(f, 0, 
SbxMAXULNG); }
+inline auto ImpDoubleToLong(double f) { return DoubleTo<sal_Int32>(f, 
SbxMINLNG, SbxMAXLNG); }
+inline auto ImpDoubleToSalUInt64(double d) { return DoubleTo<sal_uInt64>(d, 0, 
SAL_MAX_UINT64); }
+inline auto ImpDoubleToSalInt64(double d)
+{
+    return DoubleTo<sal_Int64>(d, SAL_MIN_INT64, SAL_MAX_INT64);
+}
+
 // SBXSCAN.CXX
 extern void ImpCvtNum( double nNum, short nPrec, OUString& rRes, bool 
bCoreString=false );
 extern ErrCode ImpScan
@@ -45,8 +80,6 @@ void        ImpPutInt64( SbxValues*, sal_Int64 );
 sal_uInt64  ImpGetUInt64( const SbxValues* );
 void        ImpPutUInt64( SbxValues*, sal_uInt64 );
 
-sal_Int64   ImpDoubleToSalInt64 ( double d );
-sal_uInt64  ImpDoubleToSalUInt64( double d );
 double      ImpSalUInt64ToDouble( sal_uInt64 n );
 
 // SBXLNG.CXX
diff --git a/basic/source/sbx/sbxdate.cxx b/basic/source/sbx/sbxdate.cxx
index 5ebf52b69af6..6b695a9c40c7 100644
--- a/basic/source/sbx/sbxdate.cxx
+++ b/basic/source/sbx/sbxdate.cxx
@@ -354,72 +354,24 @@ start:
         }
         break;
     case SbxBYREF | SbxCHAR:
-        if( n > SbxMAXCHAR )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXCHAR;
-        }
-        else if( n < SbxMINCHAR )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINCHAR;
-        }
-        *p->pChar = static_cast<sal_Unicode>(n);
+        *p->pChar = ImpDoubleToChar(n);
         break;
     case SbxBYREF | SbxBYTE:
-        if( n > SbxMAXBYTE )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXBYTE;
-        }
-        else if( n < 0 )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
-        }
-        *p->pByte = static_cast<sal_uInt8>(n);
+        *p->pByte = ImpDoubleToByte(n);
         break;
     case SbxBYREF | SbxINTEGER:
     case SbxBYREF | SbxBOOL:
-        if( n > SbxMAXINT )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXINT;
-        }
-        else if( n < SbxMININT )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMININT;
-        }
-        *p->pInteger = static_cast<sal_Int16>(n);
+        *p->pInteger = ImpDoubleToInteger(n);
         break;
     case SbxBYREF | SbxERROR:
     case SbxBYREF | SbxUSHORT:
-        if( n > SbxMAXUINT )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXUINT;
-        }
-        else if( n < 0 )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
-        }
-        *p->pUShort = static_cast<sal_uInt16>(n);
+        *p->pUShort = ImpDoubleToUShort(n);
         break;
     case SbxBYREF | SbxLONG:
-        if( n > SbxMAXLNG )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXLNG;
-        }
-        else if( n < SbxMINLNG )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMINLNG;
-        }
-        *p->pLong = static_cast<sal_Int32>(n);
+        *p->pLong = ImpDoubleToLong(n);
         break;
     case SbxBYREF | SbxULONG:
-        if( n > SbxMAXULNG )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = SbxMAXULNG;
-        }
-        else if( n < 0 )
-        {
-            SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
-        }
-        *p->pULong = static_cast<sal_uInt32>(n);
+        *p->pULong = ImpDoubleToULong(n);
         break;
     case SbxBYREF | SbxSINGLE:
         if( n > SbxMAXSNG )
diff --git a/basic/source/sbx/sbxdbl.cxx b/basic/source/sbx/sbxdbl.cxx
index a2fed927496d..9718c1dfd7b7 100644
--- a/basic/source/sbx/sbxdbl.cxx
+++ b/basic/source/sbx/sbxdbl.cxx
@@ -211,67 +211,19 @@ start:
             break;
         }
         case SbxBYREF | SbxCHAR:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXCHAR) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXCHAR;
-            }
-            else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMINCHAR) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMINCHAR;
-            }
-            *p->pChar = static_cast<sal_Unicode>(n); break;
+            *p->pChar = ImpDoubleToChar(n); break;
         case SbxBYREF | SbxBYTE:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXBYTE) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXBYTE;
-            }
-            else if( n < 0 )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
-            }
-            *p->pByte = static_cast<sal_uInt8>(n); break;
+            *p->pByte = ImpDoubleToByte(n); break;
         case SbxBYREF | SbxINTEGER:
         case SbxBYREF | SbxBOOL:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXINT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXINT;
-            }
-            else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMININT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMININT;
-            }
-            *p->pInteger = static_cast<sal_Int16>(n); break;
+            *p->pInteger = ImpDoubleToInteger(n); break;
         case SbxBYREF | SbxERROR:
         case SbxBYREF | SbxUSHORT:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXUINT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXUINT;
-            }
-            else if( n < 0 )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
-            }
-            *p->pUShort = static_cast<sal_uInt16>(n); break;
+            *p->pUShort = ImpDoubleToUShort(n); break;
         case SbxBYREF | SbxLONG:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXLNG) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXLNG;
-            }
-            else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMINLNG) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMINLNG;
-            }
-            *p->pLong = static_cast<sal_Int32>(n); break;
+            *p->pLong = ImpDoubleToLong(n); break;
         case SbxBYREF | SbxULONG:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXULNG) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXULNG;
-            }
-            else if( n < 0 )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
-            }
-            *p->pULong = static_cast<sal_uInt32>(n); break;
+            *p->pULong = ImpDoubleToULong(n); break;
         case SbxBYREF | SbxSINGLE:
             if( n > SbxMAXSNG )
             {
diff --git a/basic/source/sbx/sbxint.cxx b/basic/source/sbx/sbxint.cxx
index a28b810eb8c7..85febcd802c2 100644
--- a/basic/source/sbx/sbxint.cxx
+++ b/basic/source/sbx/sbxint.cxx
@@ -76,16 +76,7 @@ start:
                 nRes = static_cast<sal_Int16>(p->nULong);
             break;
         case SbxSINGLE:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(p->nSingle), 
SbxMAXINT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SbxMAXINT;
-            }
-            else if( !o3tl::convertsToAtLeast(o3tl::roundAway(p->nSingle), 
SbxMININT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SbxMININT;
-            }
-            else
-                nRes = static_cast<sal_Int16>(rtl::math::round( p->nSingle ));
+            nRes = ImpDoubleToInteger(p->nSingle);
             break;
         case SbxCURRENCY:
             {
@@ -137,16 +128,7 @@ start:
             else
                 dVal = p->nDouble;
 
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(dVal), SbxMAXINT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SbxMAXINT;
-            }
-            else if( !o3tl::convertsToAtLeast(o3tl::roundAway(dVal), 
SbxMININT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SbxMININT;
-            }
-            else
-                nRes = static_cast<sal_Int16>(rtl::math::round( dVal ));
+            nRes = ImpDoubleToInteger(dVal);
             break;
             }
         case SbxLPSTR:
@@ -160,16 +142,8 @@ start:
                 SbxDataType t;
                 if( ImpScan( *p->pOUString, d, t, nullptr, true ) != 
ERRCODE_NONE )
                     nRes = 0;
-                else if( !o3tl::convertsToAtMost(o3tl::roundAway(d), 
SbxMAXINT) )
-                {
-                    SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SbxMAXINT;
-                }
-                else if( !o3tl::convertsToAtLeast(o3tl::roundAway(d), 
SbxMININT) )
-                {
-                    SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SbxMININT;
-                }
                 else
-                    nRes = static_cast<sal_Int16>(rtl::math::round( d ));
+                    nRes = ImpDoubleToInteger(d);
             }
             break;
         case SbxOBJECT:
@@ -339,39 +313,6 @@ start:
 
 // sal_Int64 / hyper
 
-sal_Int64 ImpDoubleToSalInt64( double d )
-{
-    sal_Int64 nRes;
-    if( !o3tl::convertsToAtMost(o3tl::roundAway(d), SAL_MAX_INT64) )
-    {
-        SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SAL_MAX_INT64;
-    }
-    else if( !o3tl::convertsToAtLeast(o3tl::roundAway(d), SAL_MIN_INT64) )
-    {
-        SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SAL_MIN_INT64;
-    }
-    else
-        nRes = static_cast<sal_Int64>(rtl::math::round( d ));
-    return nRes;
-}
-
-sal_uInt64 ImpDoubleToSalUInt64( double d )
-{
-    sal_uInt64 nRes;
-    if( !o3tl::convertsToAtMost(o3tl::roundAway(d), SAL_MAX_UINT64) )
-    {
-        SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SAL_MAX_UINT64;
-    }
-    else if( d < 0.0 )
-    {
-        SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
-    }
-    else
-        nRes = static_cast<sal_uInt64>(rtl::math::round( d ));
-    return nRes;
-}
-
-
 double ImpSalUInt64ToDouble( sal_uInt64 n )
 {
     double d = 0.0;
@@ -410,11 +351,11 @@ start:
         case SbxULONG:
             nRes = static_cast<sal_Int64>(p->nULong); break;
         case SbxSINGLE:
-            nRes = static_cast<sal_Int64>(p->nSingle);
+            nRes = ImpDoubleToSalInt64(p->nSingle);
             break;
         case SbxDATE:
         case SbxDOUBLE:
-            nRes = static_cast<sal_Int64>(p->nDouble);
+            nRes = ImpDoubleToSalInt64(p->nDouble);
             break;
         case SbxCURRENCY:
             nRes = p->nInt64 / CURRENCY_FACTOR; break;
@@ -436,8 +377,7 @@ start:
                 nRes = 0;
             else
             {
-                ::OString aOStr = OUStringToOString( *p->pOUString, 
RTL_TEXTENCODING_ASCII_US );
-                nRes = aOStr.toInt64();
+                nRes = p->pOUString->toInt64();
                 if( nRes == 0 )
                 {
                     // Check if really 0 or invalid conversion
@@ -446,7 +386,7 @@ start:
                     if( ImpScan( *p->pOUString, d, t, nullptr, true ) != 
ERRCODE_NONE )
                         nRes = 0;
                     else
-                        nRes = static_cast<sal_Int64>(d);
+                        nRes = ImpDoubleToSalInt64(d);
                 }
             }
             break;
@@ -668,14 +608,11 @@ start:
         case SbxULONG:
             nRes = static_cast<sal_uInt64>(p->nULong); break;
         case SbxSINGLE:
-            nRes = static_cast<sal_uInt64>(p->nSingle); break;
+            nRes = ImpDoubleToSalUInt64(p->nSingle); break;
         case SbxDATE:
         case SbxDOUBLE:
-            {
-//TODO overflow check
-            nRes = static_cast<sal_uInt64>(p->nDouble);
+            nRes = ImpDoubleToSalUInt64(p->nDouble);
             break;
-            }
         case SbxCURRENCY:
             nRes = p->nInt64 * CURRENCY_FACTOR; break;
         case SbxSALINT64:
@@ -696,34 +633,16 @@ start:
                 nRes = 0;
             else
             {
-                ::OString aOStr = OUStringToOString
-                    ( *p->pOUString, RTL_TEXTENCODING_ASCII_US );
-                sal_Int64 n64 = aOStr.toInt64();
-                if( n64 == 0 )
+                nRes = p->pOUString->toUInt64();
+                if( nRes == 0 )
                 {
                     // Check if really 0 or invalid conversion
                     double d;
                     SbxDataType t;
                     if( ImpScan( *p->pOUString, d, t, nullptr, true ) != 
ERRCODE_NONE )
                         nRes = 0;
-                    else if( !o3tl::convertsToAtMost(o3tl::roundAway(d), 
SAL_MAX_UINT64) )
-                    {
-                        SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes 
= SAL_MAX_UINT64;
-                    }
-                    else if( d < 0.0 )
-                    {
-                        SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes 
= 0;
-                    }
                     else
-                        nRes = static_cast<sal_uInt64>(rtl::math::round( d ));
-                }
-                else if( n64 < 0 )
-                {
-                    SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
-                }
-                else
-                {
-                    nRes = n64;
+                        nRes = ImpDoubleToSalUInt64(d);
                 }
             }
             break;
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:
diff --git a/basic/source/sbx/sbxsng.cxx b/basic/source/sbx/sbxsng.cxx
index a8129b8cd48d..f97c6815d18a 100644
--- a/basic/source/sbx/sbxsng.cxx
+++ b/basic/source/sbx/sbxsng.cxx
@@ -236,90 +236,28 @@ start:
             break;
         }
         case SbxBYREF | SbxCHAR:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXCHAR) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXCHAR;
-            }
-            else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMINCHAR) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMINCHAR;
-            }
-            *p->pChar = static_cast<sal_Unicode>(n); break;
+            *p->pChar = ImpDoubleToChar(n); break;
         case SbxBYREF | SbxBYTE:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXBYTE) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXBYTE;
-            }
-            else if( n < 0 )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
-            }
-            *p->pByte = static_cast<sal_uInt8>(n); break;
+            *p->pByte = ImpDoubleToByte(n); break;
         case SbxBYREF | SbxINTEGER:
         case SbxBYREF | SbxBOOL:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXINT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXINT;
-            }
-            else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMININT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMININT;
-            }
-            *p->pInteger = static_cast<sal_Int16>(n); break;
+            *p->pInteger = ImpDoubleToInteger(n); break;
         case SbxBYREF | SbxERROR:
         case SbxBYREF | SbxUSHORT:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXUINT) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 
SbxMAXUINT;
-            }
-            else if( n < 0 )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); n = 0;
-            }
-            *p->pUShort = static_cast<sal_uInt16>(n); break;
+            *p->pUShort = ImpDoubleToUShort(n); break;
         case SbxBYREF | SbxLONG:
-        {
-            sal_Int32 i;
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXLNG) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); i = 
SbxMAXLNG;
-            }
-            else if( !o3tl::convertsToAtLeast(o3tl::roundAway(n), SbxMINLNG) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); i = 
SbxMINLNG;
-            }
-            else
-            {
-                i = sal::static_int_cast< sal_Int32 >(n);
-            }
-            *p->pLong = i; break;
-        }
+            *p->pLong = ImpDoubleToLong(n); break;
         case SbxBYREF | SbxULONG:
-        {
-            sal_uInt32 i;
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(n), SbxMAXULNG) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); i = 
SbxMAXULNG;
-            }
-            else if( n < 0 )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); i = 0;
-            }
-            else
-            {
-                i = sal::static_int_cast< sal_uInt32 >(n);
-            }
-            *p->pULong = i; break;
-        }
+            *p->pULong = ImpDoubleToULong(n); break;
         case SbxBYREF | SbxSINGLE:
             *p->pSingle = n; break;
         case SbxBYREF | SbxDATE:
         case SbxBYREF | SbxDOUBLE:
             *p->pDouble = static_cast<double>(n); break;
         case SbxBYREF | SbxSALINT64:
-            *p->pnInt64 = static_cast<sal_Int64>(n); break;
+            *p->pnInt64 = ImpDoubleToSalInt64(n); break;
         case SbxBYREF | SbxSALUINT64:
-            *p->puInt64 = static_cast<sal_uInt64>(n); break;
+            *p->puInt64 = ImpDoubleToSalUInt64(n); break;
         case SbxBYREF | SbxCURRENCY:
             double d;
             if( n > SbxMAXCURR )
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:
diff --git a/basic/source/sbx/sbxulng.cxx b/basic/source/sbx/sbxulng.cxx
index bcb1ce0f2778..fa872b8b0bb5 100644
--- a/basic/source/sbx/sbxulng.cxx
+++ b/basic/source/sbx/sbxulng.cxx
@@ -65,16 +65,7 @@ start:
         case SbxULONG:
             nRes = p->nULong; break;
         case SbxSINGLE:
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(p->nSingle), 
SbxMAXULNG) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SbxMAXULNG;
-            }
-            else if( p->nSingle < 0 )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
-            }
-            else
-                nRes = static_cast<sal_uInt32>( p->nSingle + 0.5 );
+            nRes = ImpDoubleToULong(p->nSingle);
             break;
         case SbxDATE:
         case SbxDOUBLE:
@@ -100,16 +91,7 @@ start:
             else
                 dVal = p->nDouble;
 
-            if( !o3tl::convertsToAtMost(o3tl::roundAway(dVal), SbxMAXULNG) )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SbxMAXULNG;
-            }
-            else if( dVal < 0 )
-            {
-                SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
-            }
-            else
-                nRes = static_cast<sal_uInt32>( dVal + 0.5 );
+            nRes = ImpDoubleToULong(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), 
SbxMAXULNG) )
-                {
-                    SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 
SbxMAXULNG;
-                }
-                else if( d < 0 )
-                {
-                    SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = 0;
-                }
                 else
-                    nRes = static_cast<sal_uInt32>( d + 0.5 );
+                    nRes = ImpDoubleToULong(d);
             }
             break;
         case SbxOBJECT:

Reply via email to