>From the commit message:

CRT libraries other then the UCRT can open stderr in full-buffering
mode. This happens for example when output goes to a pipe. The C
standard disallow such mode on stderr (C23 7.23.2 pt. 7) [1]:

> as initially opened, the standard error stream is not fully buffered

Here we ensure that stderr is unbuffered. Note that we can't use line
buffering since it's the same as full buffering [2]:

> _IOLBF: For some systems this mode provides line buffering.
  However on Win32 the behavior is the same as _IOFBF - Full Buffering.

References:

1. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsection.7.23.3
2. 
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setvbuf?view=msvc-170
3. https://sourceforge.net/p/mingw/mailman/message/27121137/
From f39e6cf1284319be53bc4519d28f375f38875ad9 Mon Sep 17 00:00:00 2001
From: Luca Bacci <[email protected]>
Date: Sat, 13 Dec 2025 14:22:58 +0100
Subject: [PATCH] Ensure that stderr is not fully buffered

CRT libraries other then the UCRT can open stderr in full-buffering
mode. This happens for example when output goes to a pipe. The C
standard disallow such mode on stderr (C23 7.23.2 pt. 7) [1]:

> as initially opened, the standard error stream is not fully buffered

Here we ensure that stderr is unbuffered. Note that we can't use line
buffering since it's the same as full buffering [2]:

> _IOLBF: For some systems this mode provides line buffering.
  However on Win32 the behavior is the same as _IOFBF - Full Buffering.

References:

1. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsection.7.23.3
2. 
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setvbuf?view=msvc-170
3. https://sourceforge.net/p/mingw/mailman/message/27121137/

Signed-off-by: Luca Bacci <[email protected]>
---
 mingw-w64-crt/crt/crtexe.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index 94bad6aa..d7497ef0 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -199,6 +199,17 @@ __tmainCRTStartup (void)
        if (__globallocalestatus == -1)
          _configthreadlocale (-1);
 
+#if !defined (_UCRT)
+       /* Before the UCRT stderr could be opened in full buffering
+       * mode, for example when output goes to a pipe.
+       *
+       * The C standard disallow full buffering on stderr. Note
+       * that line buffering is the same as full buffering in the
+       * Windows CRT, so we have to disable buffering altogether.
+       */
+       setvbuf (stderr, NULL, _IONBF, 0);
+#endif
+
        if (_initterm_e (__xi_a, __xi_z) != 0)
          return 255;
 
-- 
2.49.0.windows.1

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to