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

Tom Anderson via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 22 16:17:56 PDT 2019


thomasanderson created this revision.
thomasanderson added a reviewer: EricWF.
Herald added a subscriber: ldionne.

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.


Repository:
  rCXX libc++

https://reviews.llvm.org/D59727

Files:
  src/support/win32/locale_win32.cpp


Index: src/support/win32/locale_win32.cpp
===================================================================
--- src/support/win32/locale_win32.cpp
+++ src/support/win32/locale_win32.cpp
@@ -93,7 +93,9 @@
     va_list ap;
     va_start( ap, format );
 #if defined(_LIBCPP_MSVCRT)
-    int result = _vsnprintf_l( ret, n, format, loc, ap );
+    int result = __stdio_common_vsprintf(
+        _CRT_INTERNAL_LOCAL_PRINTF_OPTIONS | _CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR,
+        ret, n, format, loc, ap);
 #else
     int result = vsnprintf( ret, n, format, ap );
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59727.191971.patch
Type: text/x-patch
Size: 579 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190322/8e2eec8f/attachment.bin>


More information about the libcxx-commits mailing list