Branch
Hash :
84d76cdd
Author :
Date :
2025-09-17T09:31:31
stdio-h: Work around [v][f]printf bugs in mingw with msvcrt. Reported by 松延 英樹 <maznobu@gmail.com> in <https://github.com/mlocati/gettext-iconv-windows/issues/52>. * lib/stdio.in.h (gl_consolesafe_fprintf, gl_consolesafe_printf, gl_consolesafe_vfprintf, gl_consolesafe_vprintf): New declarations. (fprintf): When msvcrt is in use, use gl_consolesafe_fprintf. (printf): When msvcrt is in use, use gl_consolesafe_printf. (vfprintf): When msvcrt is in use, use gl_consolesafe_vfprintf. (vprintf): When msvcrt is in use, use gl_consolesafe_vprintf. * lib/stdio-consolesafe.c: Include fseterr.h. (gl_consolesafe_fprintf, gl_consolesafe_printf, gl_consolesafe_vfprintf, gl_consolesafe_vprintf): New functions. * lib/stdio-write.c (vfprintf): When msvcrt is in use, use gl_consolesafe_vfprintf. * modules/stdio-h (Depends-on): Add fseterr. * doc/posix-functions/fprintf.texi: Document the mingw bug. * doc/posix-functions/printf.texi: Likewise. * doc/posix-functions/vfprintf.texi: Likewise. * doc/posix-functions/vprintf.texi: Likewise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/* msvcrt workarounds.
Copyright (C) 2025 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
/* Specification. */
#include <stdio.h>
#include <stdckdint.h>
#include <stdlib.h>
#include <string.h>
/* Outputs N bytes starting at S to FP.
These N bytes are known to be followed by a NUL.
Finally frees the string at S.
Returns the number of written bytes. */
static size_t
workaround_fwrite0 (char *s, size_t n, FILE *fp)
{
const char *ptr = s;
/* Use fputs instead of fwrite, which is buggy in msvcrt. */
size_t written = 0;
while (n > 0)
{
size_t l = strlen (ptr); /* 0 <= l <= n */
if (l > 0)
{
if (fputs (ptr, fp) == EOF)
break;
written += l;
n -= l;
}
if (n == 0)
break;
if (fputc ('\0', fp) == EOF)
break;
written++;
n--;
ptr += l + 1;
}
free (s);
return written;
}
size_t
gl_consolesafe_fwrite (const void *ptr, size_t size, size_t nmemb, FILE *fp)
{
size_t nbytes;
if (ckd_mul (&nbytes, size, nmemb) || nbytes == 0)
/* Overflow, or nothing to do. */
return 0;
char *tmp = malloc (nbytes + 1);
if (tmp == NULL)
return 0;
memcpy (tmp, ptr, nbytes);
tmp[nbytes] = '\0';
size_t written = workaround_fwrite0 (tmp, nbytes, fp);
return written / size;
}
#if defined __MINGW32__ && __USE_MINGW_ANSI_STDIO
# include "fseterr.h"
/* Bypass the functions __mingw_[v][f]printf, that trigger a bug in msvcrt,
but without losing the support for modern format specifiers added by
__mingw_*printf. */
int
gl_consolesafe_fprintf (FILE *restrict fp, const char *restrict format, ...)
{
va_list args;
char *tmpstring;
va_start (args, format);
int result = vasprintf (&tmpstring, format, args);
va_end (args);
if (result >= 0)
{
if (workaround_fwrite0 (tmpstring, result, fp) < result)
result = -1;
}
else
fseterr (fp);
return result;
}
int
gl_consolesafe_printf (const char *restrict format, ...)
{
va_list args;
char *tmpstring;
va_start (args, format);
int result = vasprintf (&tmpstring, format, args);
va_end (args);
if (result >= 0)
{
if (workaround_fwrite0 (tmpstring, result, stdout) < result)
result = -1;
}
else
fseterr (stdout);
return result;
}
int
gl_consolesafe_vfprintf (FILE *restrict fp,
const char *restrict format, va_list args)
{
char *tmpstring;
int result = vasprintf (&tmpstring, format, args);
if (result >= 0)
{
if (workaround_fwrite0 (tmpstring, result, fp) < result)
result = -1;
}
else
fseterr (fp);
return result;
}
int
gl_consolesafe_vprintf (const char *restrict format, va_list args)
{
char *tmpstring;
int result = vasprintf (&tmpstring, format, args);
if (result >= 0)
{
if (workaround_fwrite0 (tmpstring, result, stdout) < result)
result = -1;
}
else
fseterr (stdout);
return result;
}
#endif