summaryrefslogtreecommitdiff
path: root/sal/rtl
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2022-02-25 13:20:14 +0100
committerStephan Bergmann <sbergman@redhat.com>2022-02-25 22:47:21 +0100
commit7025ca508b427fdf5eee7410d7e636adac9f0a94 (patch)
tree27e22d4d8ea7e3660e89801d14c605e87f7e81b2 /sal/rtl
parentb14bb255199e7d6db6ec9155b5d9237cb35fdba7 (diff)
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 <sbergman@redhat.com>
Diffstat (limited to 'sal/rtl')
-rw-r--r--sal/rtl/ustring.cxx11
1 files changed, 10 insertions, 1 deletions
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
}
}