sal/qa/rtl/oustring/rtl_OUString2.cxx | 11 ++++++++++- sal/rtl/ustring.cxx | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-)
New commits: commit 7025ca508b427fdf5eee7410d7e636adac9f0a94 Author: Stephan Bergmann <sberg...@redhat.com> AuthorDate: Fri Feb 25 13:20:14 2022 +0100 Commit: Stephan Bergmann <sberg...@redhat.com> CommitDate: Fri Feb 25 22:47:21 2022 +0100 Make an -fsanitize=undefined workaround conditional ...that had been introduced with b5cb4935c268f12e63b61e035b455b0a59e67aa2 "Work around undef conversion of large double to float" but should no longer be necessary with <https://github.com/llvm/llvm-project/commit/9e52c43090f8cd980167bbd2719878ae36bcf6b5> "Treat the range of representable values of floating-point types as [-inf, +inf] not as [-max, +max]" added towards Clang 9. Thanks to Mike Kaganski for pointing me at this old code and at Richard Smith's comment at <https://cplusplusmusings.wordpress.com/2013/03/26/testing-libc-with-fsanitizeundefined/>. Change-Id: I8ecf115fcf6b1ebf621cb4567f8d31ac9b10ef1c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130531 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sberg...@redhat.com> diff --git a/sal/qa/rtl/oustring/rtl_OUString2.cxx b/sal/qa/rtl/oustring/rtl_OUString2.cxx index 0e85a066f33f..0ce047e9fb32 100644 --- a/sal/qa/rtl/oustring/rtl_OUString2.cxx +++ b/sal/qa/rtl/oustring/rtl_OUString2.cxx @@ -30,6 +30,7 @@ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/plugin/TestPlugIn.h> +#include <config_options.h> #include <o3tl/cppunittraitshelper.hxx> #include <stringhelper.hxx> @@ -41,14 +42,22 @@ namespace rtl_OUString namespace { // Avoid -fsanitize=undefined warning e.g. "runtime error: value 1e+99 is -// outside the range of representable values of type 'float'": +// outside the range of representable values of type 'float'" with Clang prior to +// <https://github.com/llvm/llvm-project/commit/9e52c43090f8cd980167bbd2719878ae36bcf6b5> "Treat the +// range of representable values of floating-point types as [-inf, +inf] not as [-max, +max]" +// (ENABLE_RUNTIME_OPTIMIZATIONS is an approximation for checking whether building is done without +// -fsanitize=undefined): float doubleToFloat(double x) { +#if !defined __clang__ || __clang_major__ >= 9 || ENABLE_RUNTIME_OPTIMIZATIONS + return static_cast<float>(x); +#else return x < -std::numeric_limits<float>::max() ? -std::numeric_limits<float>::infinity() : x > std::numeric_limits<float>::max() ? std::numeric_limits<float>::infinity() : static_cast<float>(x); +#endif } } diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx index aa3a7263d1b7..1f81dc3fd902 100644 --- a/sal/rtl/ustring.cxx +++ b/sal/rtl/ustring.cxx @@ -25,6 +25,7 @@ #include <stdexcept> #include <string> +#include <config_options.h> #include <osl/diagnose.h> #include <osl/interlck.h> #include <osl/mutex.h> @@ -133,14 +134,22 @@ sal_Int32 SAL_CALL rtl_ustr_valueOfDouble(sal_Unicode * pStr, double d) namespace { // Avoid -fsanitize=undefined warning e.g. "runtime error: value 1e+99 is -// outside the range of representable values of type 'float'": +// outside the range of representable values of type 'float'" with Clang prior to +// <https://github.com/llvm/llvm-project/commit/9e52c43090f8cd980167bbd2719878ae36bcf6b5> "Treat the +// range of representable values of floating-point types as [-inf, +inf] not as [-max, +max]" +// (ENABLE_RUNTIME_OPTIMIZATIONS is an approximation for checking whether building is done without +// -fsanitize=undefined): float doubleToFloat(double x) { +#if !defined __clang__ || __clang_major__ >= 9 || ENABLE_RUNTIME_OPTIMIZATIONS + return static_cast<float>(x); +#else return x < -std::numeric_limits<float>::max() ? -std::numeric_limits<float>::infinity() : x > std::numeric_limits<float>::max() ? std::numeric_limits<float>::infinity() : static_cast<float>(x); +#endif } }