Format %C and %S uses opposite wideness as format %c and %s.
So %S in printf uses wchar_t* string and in wprintf uses char* string.

Format %C and %S can be changed by l or h modifiers, like %c and %s.
So for example %hC uses char type in both printf and wprintf calls
and for example %lS uses wchar_t* string in both swprintf and sprintf.

This change fixes the mingw_pformat function to correctly process %C and %S
formats with optional l and h modifiers. This also aligns mingw-w64 support
with crtdll, msvcrt and UCRT *printf functions.
---
 mingw-w64-crt/stdio/mingw_pformat.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/mingw-w64-crt/stdio/mingw_pformat.c 
b/mingw-w64-crt/stdio/mingw_pformat.c
index d094e7ace25d..8ac4b7b583c5 100644
--- a/mingw-w64-crt/stdio/mingw_pformat.c
+++ b/mingw-w64-crt/stdio/mingw_pformat.c
@@ -2438,10 +2438,18 @@ __pformat (int flags, void *dest, int max, const 
APICHAR *fmt, va_list argv)
 
           case 'C':
             /*
-             * Equivalent to `%lc'; set `length' accordingly,
-             * and simply fall through.
+             * If the explicit length modifier is not set
+             * then it is opposite of the default length
+             * modifier used by `%c`.
              */
-            length = PFORMAT_LENGTH_LONG;
+            if( length == PFORMAT_LENGTH_INT )
+            {
+    #ifndef __BUILD_WIDEAPI
+              length = PFORMAT_LENGTH_LONG;
+    #else
+              length = PFORMAT_LENGTH_SHORT;
+    #endif
+            }
 
             /* fallthrough */
 
@@ -2484,10 +2492,18 @@ __pformat (int flags, void *dest, int max, const 
APICHAR *fmt, va_list argv)
 
           case 'S':
             /*
-             * Equivalent to `%ls'; set `length' accordingly,
-             * and simply fall through.
+             * If the explicit length modifier is not set
+             * then it is opposite of the default length
+             * modifier used by `%s`.
              */
-            length = PFORMAT_LENGTH_LONG;
+            if( length == PFORMAT_LENGTH_INT )
+            {
+    #ifndef __BUILD_WIDEAPI
+              length = PFORMAT_LENGTH_LONG;
+    #else
+              length = PFORMAT_LENGTH_SHORT;
+    #endif
+            }
 
             /* fallthrough */
 
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to