Original MSVC 6.0 msvcrt.dll library does not provide _vscprintf()
function. Therefore usage of snprintf() with this DLL library cause
application crash. Add simple fallback implementation of _vscprintf() just
for __ms_vsnprintf() to allow usage of snprintf() function also when using
original msvcrt.dll library. This fallback implementation is static and not
exported outside of vsnprintf.c source file.
---
mingw-w64-crt/stdio/vsnprintf.c | 67 +++++++++++++++++++++++++++++++++
1 file changed, 67 insertions(+)
diff --git a/mingw-w64-crt/stdio/vsnprintf.c b/mingw-w64-crt/stdio/vsnprintf.c
index 364186702b75..d65ebd3be9e2 100644
--- a/mingw-w64-crt/stdio/vsnprintf.c
+++ b/mingw-w64-crt/stdio/vsnprintf.c
@@ -4,8 +4,75 @@
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
*/
#define __CRT__NO_INLINE
+#include <windows.h>
+#include <msvcrt.h>
#include <stdarg.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+static int __cdecl my_vscprintf_init(const char * __restrict__ format, va_list
arglist);
+static int (__cdecl *my_vscprintf_func)(const char * __restrict__, va_list) =
my_vscprintf_init;
+
+/* emulation of _vscprintf() via _vsnprintf() */
+static int __cdecl my_vscprintf_emu(const char * __restrict__ format, va_list
arglist)
+{
+ char *buffer, *new_buffer;
+ size_t size;
+ int ret;
+
+ /* if format is a null pointer, _vscprintf() returns -1 and sets errno to
EINVAL */
+ if (!format) {
+ _set_errno(EINVAL);
+ return -1;
+ }
+
+ /* size for _vsnprintf() must be non-zero and buffer must have place for
terminating null character */
+ size = strlen(format) * 2 + 1;
+ buffer = malloc(size);
+
+ if (!buffer) {
+ _set_errno(ENOMEM);
+ return -1;
+ }
+
+ /* if the number of characters to write is greater than size, _vsnprintf()
returns -1 */
+ while (size < SIZE_MAX/2 && (ret = _vsnprintf(buffer, size, format,
arglist)) < 0) {
+ /* in this case try with larger buffer */
+ size *= 2;
+ new_buffer = realloc(buffer, size);
+ if (!new_buffer)
+ break;
+ buffer = new_buffer;
+ }
+
+ free(buffer);
+
+ if (ret < 0) {
+ _set_errno(ENOMEM);
+ return -1;
+ }
+
+ return ret;
+}
+
+static int __cdecl my_vscprintf_init(const char * __restrict__ format, va_list
arglist)
+{
+ HMODULE msvcrt = __mingw_get_msvcrt_handle();
+ int (__cdecl *func)(const char * __restrict__, va_list) = NULL;
+
+ if (msvcrt)
+ func = (int (__cdecl *)(const char * __restrict__,
va_list))GetProcAddress(msvcrt, "_vscprintf");
+
+ if (!func)
+ func = my_vscprintf_emu;
+
+ return (my_vscprintf_func = func)(format, arglist);
+}
+
+#undef _vscprintf
+#define _vscprintf my_vscprintf_func
int __cdecl __ms_vsnprintf (char *s,size_t n,const char *format,va_list arg)
{
--
2.20.1
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public