When `wcrtomb()` is called with a null wide character (second argument) it is
required to output a sequence of bytes to 'restore the initial shift state'.
For this function, the `mbstate_t` structure shall be initialized before the
call.

Previous code passed an uninitialized structure and expected `wcrtomb(buf,
L'\0', &state)` to initialize `state` to the initial shift state, which was
incorrect.

Signed-off-by: LIU Hao <lh_mo...@126.com>
---
 mingw-w64-crt/stdio/mingw_pformat.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mingw-w64-crt/stdio/mingw_pformat.c 
b/mingw-w64-crt/stdio/mingw_pformat.c
index d094e7ace..4e41b4d82 100644
--- a/mingw-w64-crt/stdio/mingw_pformat.c
+++ b/mingw-w64-crt/stdio/mingw_pformat.c
@@ -569,8 +569,8 @@ void __pformat_wputchars( const wchar_t *s, int count, 
__pformat_t *stream )
    * output quota is honoured.
    */
   char buf[16];
-  mbstate_t state;
-  int len = wcrtomb(buf, L'\0', &state);
+  mbstate_t state = {0};
+  int len;
    if( (stream->precision >= 0) && (count > stream->precision) )
     /*
--
2.50.1

From daca17774fe8ac95136a779825a46e629c42861a Mon Sep 17 00:00:00 2001
From: LIU Hao <lh_mo...@126.com>
Date: Fri, 25 Jul 2025 17:40:23 +0800
Subject: [PATCH] crt/mingw_pformat: Initialize `mbstate_t` structure properly

When `wcrtomb()` is called with a null wide character (second argument) it is
required to output a sequence of bytes to 'restore the initial shift state'.
For this function, the `mbstate_t` structure shall be initialized before the
call.

Previous code passed an uninitialized structure and expected `wcrtomb(buf,
L'\0', &state)` to initialize `state` to the initial shift state, which was
incorrect.

Signed-off-by: LIU Hao <lh_mo...@126.com>
---
 mingw-w64-crt/stdio/mingw_pformat.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mingw-w64-crt/stdio/mingw_pformat.c 
b/mingw-w64-crt/stdio/mingw_pformat.c
index d094e7ace..4e41b4d82 100644
--- a/mingw-w64-crt/stdio/mingw_pformat.c
+++ b/mingw-w64-crt/stdio/mingw_pformat.c
@@ -569,8 +569,8 @@ void __pformat_wputchars( const wchar_t *s, int count, 
__pformat_t *stream )
    * output quota is honoured.
    */
   char buf[16];
-  mbstate_t state;
-  int len = wcrtomb(buf, L'\0', &state);
+  mbstate_t state = {0};
+  int len;
 
   if( (stream->precision >= 0) && (count > stream->precision) )
     /*
-- 
2.50.1

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

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

Reply via email to