From 50a1929f6c0a8509b0b695b3aac25fbf70b8ffd6 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 6 Nov 2025 12:11:48 -0800 Subject: [PATCH] Update from Gnulib by running admin/merge-gnulib --- lib/stdio-consolesafe.c | 50 +++++++++++++++++++++++++++++++++++++++++ m4/gnulib-comp.m4 | 1 + 2 files changed, 51 insertions(+) diff --git a/lib/stdio-consolesafe.c b/lib/stdio-consolesafe.c index fbea20be224..b5ca8cc0125 100644 --- a/lib/stdio-consolesafe.c +++ b/lib/stdio-consolesafe.c @@ -75,6 +75,56 @@ gl_consolesafe_fwrite (const void *ptr, size_t size, size_t nmemb, FILE *fp) # include "fseterr.h" +# if !HAVE_VASPRINTF + +# include +# include + +/* The old mingw (before mingw-w64) does not have the vasprintf function. + Define a suitable replacement here, that supports the same format + specifiers as the mingw *printf functions. */ + +static int +vasprintf (char **resultp, const char *format, va_list args) +{ + /* First try: Use a stack-allocated buffer. */ + char buf[2048]; + size_t bufsize = sizeof (buf); + int ret = __mingw_vsnprintf (buf, bufsize, format, args); + if (ret < 0) + return -1; + size_t nbytes = ret; + char *mem = (char *) malloc (nbytes + 1); + if (mem == NULL) + { + errno = ENOMEM; + return -1; + } + if (ret < bufsize) + { + /* The buffer was sufficiently large. */ + memcpy (mem, buf, nbytes + 1); + } + else + { + /* Second try: Use the heap-allocated memory. */ + ret = __mingw_vsnprintf (mem, nbytes + 1, format, args); + if (ret < 0) + { + int saved_errno = errno; + free (mem); + errno = saved_errno; + return -1; + } + if (ret != nbytes) + abort (); + } + *resultp = mem; + return nbytes; +} + +# endif + /* 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. */ diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4 index fea8b7d684b..3199de24209 100644 --- a/m4/gnulib-comp.m4 +++ b/m4/gnulib-comp.m4 @@ -608,6 +608,7 @@ AC_DEFUN([gl_INIT], ;; esac gl_CONDITIONAL([GL_COND_OBJ_STDIO_CONSOLESAFE], [test $USES_MSVCRT = 1]) + AC_CHECK_FUNCS([vasprintf]) gl_STDLIB_H gl_STDLIB_H_REQUIRE_DEFAULTS AC_PROG_MKDIR_P