[libcxx-commits] [libcxx] r357024 - [libc++] Fix return value of snprintf_l() on Windows when buffer is too small

Thomas Anderson via libcxx-commits libcxx-commits at lists.llvm.org
Tue Mar 26 12:51:30 PDT 2019


Author: thomasanderson
Date: Tue Mar 26 12:51:30 2019
New Revision: 357024

URL: http://llvm.org/viewvc/llvm-project?rev=357024&view=rev
Log:
[libc++] Fix return value of snprintf_l() on Windows when buffer is too small

When the output buffer is too small to contain the output, `vsnprintf()`
fills the buffer and returns the number of characters that __would have__
been written if the buffer was sufficiently large.

`_vnsprintf_s()` on the other hand fills the buffer and returns -1 when this
happens.  We want the former behavior, but we also want to be able to
pass in a locale to prevent having to call `setlocale()`.

`__stdio_common_vsprintf()` is the only function general enough to get
the behavior we want.

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

Modified:
    libcxx/trunk/src/support/win32/locale_win32.cpp

Modified: libcxx/trunk/src/support/win32/locale_win32.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/support/win32/locale_win32.cpp?rev=357024&r1=357023&r2=357024&view=diff
==============================================================================
--- libcxx/trunk/src/support/win32/locale_win32.cpp (original)
+++ libcxx/trunk/src/support/win32/locale_win32.cpp Tue Mar 26 12:51:30 2019
@@ -87,14 +87,15 @@ int wctob_l( wint_t c, locale_t loc )
 
 int snprintf_l(char *ret, size_t n, locale_t loc, const char *format, ...)
 {
-#if !defined(_LIBCPP_MSVCRT)
-    __libcpp_locale_guard __current(loc);
-#endif
     va_list ap;
     va_start( ap, format );
 #if defined(_LIBCPP_MSVCRT)
-    int result = _vsnprintf_l( ret, n, format, loc, ap );
+    // FIXME: Remove usage of internal CRT function and globals.
+    int result = __stdio_common_vsprintf(
+        _CRT_INTERNAL_LOCAL_PRINTF_OPTIONS | _CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR,
+        ret, n, format, loc, ap);
 #else
+    __libcpp_locale_guard __current(loc);
     int result = vsnprintf( ret, n, format, ap );
 #endif
     va_end(ap);




More information about the libcxx-commits mailing list