Skip to content

Commit

Permalink
[compiler-rt] [ubsan] Fix printing of floats in mingw mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mstorsjo committed Apr 13, 2023
1 parent 6d7d0ed commit fb012c1
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler-rt/lib/ubsan/ubsan_diag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fb012c1

Please sign in to comment.