Files
loongoffice/include/tools/helpers.hxx
Stephan Bergmann dcd9e98383 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:🫘: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 <sbergman@redhat.com>
2022-02-07 23:48:59 +01:00

105 lines
2.4 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <sal/config.h>
#include <sal/types.h>
#include <tools/long.hxx>
#include <cassert>
#include <limits>
#include <type_traits>
template<typename T>
inline
typename std::enable_if<
std::is_signed<T>::value || std::is_floating_point<T>::value, long >::type
MinMax(T nVal, tools::Long nMin, tools::Long nMax)
{
assert(nMin <= nMax);
if (nVal >= nMin)
{
if (nVal <= nMax)
return static_cast<tools::Long>(nVal);
else
return nMax;
}
else
{
return nMin;
}
}
template<typename T>
inline
typename std::enable_if<
std::is_unsigned<T>::value, long >::type
MinMax(T nVal, tools::Long nMin, tools::Long nMax)
{
assert(nMin <= nMax);
if (nMax < 0)
{
return nMax;
}
else
{
if (nMin < 0 || nVal >= static_cast<unsigned long>(nMin))
{
if (nVal <= static_cast<unsigned long>(nMax))
return static_cast<tools::Long>(nVal);
else
return nMax;
}
else
{
return nMin;
}
}
}
inline sal_uInt32 AlignedWidth4Bytes(sal_uInt32 nWidthBits)
{
if (nWidthBits > SAL_MAX_UINT32 - 31)
nWidthBits = SAL_MAX_UINT32;
else
nWidthBits += 31;
return (nWidthBits >> 5) << 2;
}
inline tools::Long FRound( double fVal )
{
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]
template <typename T>
[[nodiscard]] inline typename std::enable_if<std::is_signed<T>::value, T>::type
NormAngle180(T angle)
{
while (angle <= -180)
angle += 360;
while (angle > 180)
angle -= 360;
return angle;
}
//valid range: [0,360)
template <typename T> [[nodiscard]] inline T NormAngle360(T angle)
{
while (angle < 0)
angle += 360;
while (angle >= 360)
angle -= 360;
return angle;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */