From: Martin Storsjö Date: Wed, 12 Apr 2023 08:38:05 +0000 (+0000) Subject: [compiler-rt] [ubsan] Fix printing of floats in mingw mode X-Git-Tag: upstream/17.0.6~11803 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fb012c1eeb50500bbd1f289b50aa08d1e2f10098;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/compiler-rt/lib/ubsan/ubsan_diag.cpp b/compiler-rt/lib/ubsan/ubsan_diag.cpp index 3673e66..dd99613 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