[compiler-rt] fb012c1 - [compiler-rt] [ubsan] Fix printing of floats in mingw mode

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 13 02:06:23 PDT 2023


Author: Martin Storsjö
Date: 2023-04-13T12:03:34+03:00
New Revision: fb012c1eeb50500bbd1f289b50aa08d1e2f10098

URL: https://github.com/llvm/llvm-project/commit/fb012c1eeb50500bbd1f289b50aa08d1e2f10098
DIFF: https://github.com/llvm/llvm-project/commit/fb012c1eeb50500bbd1f289b50aa08d1e2f10098.diff

LOG: [compiler-rt] [ubsan] Fix printing of floats in mingw mode

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

Added: 
    

Modified: 
    compiler-rt/lib/ubsan/ubsan_diag.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/ubsan/ubsan_diag.cpp b/compiler-rt/lib/ubsan/ubsan_diag.cpp
index 3673e66539d0e..dd99613abbe3f 100644
--- a/compiler-rt/lib/ubsan/ubsan_diag.cpp
+++ b/compiler-rt/lib/ubsan/ubsan_diag.cpp
@@ -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


        


More information about the llvm-commits mailing list