include/tools/helpers.hxx |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

New commits:
commit dcd9e98383c767bbb6014c6ca5fe786579eda08f
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Mon Feb 7 18:59:20 2022 +0100
Commit:     Stephan Bergmann <sberg...@redhat.com>
CommitDate: Mon Feb 7 23:48:59 2022 +0100

    float-cast-overflow
    
    ...after ea771e85b2302829394df545bb82c02bff2750c2 "Resolves: tdf#146997 use
    sal_Int64 instead of sal_Int32 for spinbutton values" changed the default 
values
    of pcr::ONumericControl::setMin/MaxValue
    (extensions/source/propctrlr/standardcontrol.cxx) from int to sal_Int64 
limits.
    
    With SAL_USE_VCLPLUGIN=gtk3, in Writer creating a "Form - Label" and 
selecting
    "Control Properties" in its context menu caused
    
    > include/tools/helpers.hxx:75:93: runtime error: 9.22337e+18 is outside 
the range of representable values of type 'long'
    >  #0 in FRound(double) at include/tools/helpers.hxx:75:93
    >  #1 in (anonymous namespace)::GtkInstanceSpinButton::fromGtk(double) 
const at vcl/unx/gtk3/gtkinst.cxx:16443:16
    >  #2 in (anonymous namespace)::GtkInstanceSpinButton::get_range(long&, 
long&) const at vcl/unx/gtk3/gtkinst.cxx:16521:15
    >  #3 in weld::MetricSpinButton::update_width_chars() at 
vcl/source/window/builder.cxx:258:24
    >  #4 in weld::MetricSpinButton::set_range(long, long, FieldUnit) at 
include/vcl/weld.hxx:1991:9
    >  #5 in weld::MetricSpinButton::set_min(long, FieldUnit) at 
include/vcl/weld.hxx:2005:9
    >  #6 in 
pcr::ONumericControl::setMinValue(com::sun::star::beans::Optional<double> 
const&) at extensions/source/propctrlr/standardcontrol.cxx:401:38
    
    and similarly for pcr::ONumericControl::setMaxValue:
    
    For one, for fVal = -9223372036854775808 (i.e., -2^63), -fVal + 0.5 (i.e.,
    9223372036854775808.5) was outside the range of 64-bit tools::Long, so 
rewrite
    the non-positive branch in a simpler way (and I have no idea why it was 
written
    in the more complex way in the first place).
    
    For another, for fVal = 9223372036854775807 (i.e., 2^63 - 1), fVal + 0.5 
(i.e.,
    9223372036854775807.5) cannot be represented as IEEE 754 double exactly and 
is
    represented as 9223372036854775808, which was similarly outside the range of
    64-bit tools::Long, so treat that limit value specially on the positive 
branch.
    
    Change-Id: Ic2a553e9ce6319eac591058b1218b29dffaeab2b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129638
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>

diff --git a/include/tools/helpers.hxx b/include/tools/helpers.hxx
index 9340bf9f7294..5f61ba80836c 100644
--- a/include/tools/helpers.hxx
+++ b/include/tools/helpers.hxx
@@ -12,6 +12,7 @@
 #include <sal/types.h>
 #include <tools/long.hxx>
 #include <cassert>
+#include <limits>
 #include <type_traits>
 
 template<typename T>
@@ -72,7 +73,10 @@ inline sal_uInt32 AlignedWidth4Bytes(sal_uInt32 nWidthBits)
 
 inline tools::Long FRound( double fVal )
 {
-    return fVal > 0.0 ? static_cast<tools::Long>( fVal + 0.5 ) : 
-static_cast<tools::Long>( -fVal + 0.5 );
+    return fVal > 0.0
+        ? fVal == double(std::numeric_limits<tools::Long>::max())
+            ? std::numeric_limits<tools::Long>::max() : 
static_cast<tools::Long>( fVal + 0.5 )
+        :  static_cast<tools::Long>( fVal - 0.5 );
 }
 
 //valid range:  (-180,180]

Reply via email to