[compiler-rt] [ubsan] Fix printing of floats in mingw mode
authorMartin Storsjö <martin@martin.st>
Wed, 12 Apr 2023 08:38:05 +0000 (08:38 +0000)
committerMartin Storsjö <martin@martin.st>
Thu, 13 Apr 2023 09:03:34 +0000 (12:03 +0300)
In mingw mode on x86, long doubles are 80 bit - while MSVC mode uses
long doubles that are equal to regular doubles (on all architectures).

In the case of this formatting function, we're calling a MS CRT
provided printf function which interprets long doubles as 64 bit.

Since the long doubles are equal to regular doubles on all MSVC
platforms, just use regular double formatting. For MSVC environments
there's no difference, but for mingw environments, this avoids the
ambiguity.

Differential Revision: https://reviews.llvm.org/D148133

compiler-rt/lib/ubsan/ubsan_diag.cpp

index 3673e66..dd99613 100644 (file)
@@ -214,7 +214,12 @@ static void RenderText(InternalScopedString *Buffer, const char *Message,
       //        printf, and stop using snprintf here.
       char FloatBuffer[32];
 #if SANITIZER_WINDOWS
-      sprintf_s(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
+      // On MSVC platforms, long doubles are equal to regular doubles.
+      // In MinGW environments on x86, long doubles are 80 bit, but here,
+      // we're calling an MS CRT provided printf function which considers
+      // long doubles to be 64 bit. Just cast the float value to a regular
+      // double to avoid the potential ambiguity in MinGW mode.
+      sprintf_s(FloatBuffer, sizeof(FloatBuffer), "%g", (double)A.Float);
 #else
       snprintf(FloatBuffer, sizeof(FloatBuffer), "%Lg", (long double)A.Float);
 #endif