From b11062c28d3b8b9401effcc9fd7a7f76d920461f Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 26 Mar 2012 12:47:40 +0100 Subject: MinGW snprintf/vsnprintf work properly --- include/mingw.h | 12 ++++++++++++ libbb/platform.c | 15 +-------------- libbb/xfuncs_printf.c | 14 -------------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index 89268d62b..b281a4b49 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -1,5 +1,17 @@ #include +/* + * MS Windows has broken versions of snprintf/vsnprintf that return -1 if + * the string is too large for the buffer and don't null terminate the + * string if it fills the buffer. However, if we're using a sufficiently + * modern version of MinGW and have an appropriate source code qualifier + * (such as _GNU_SOURCE) MinGW will use its own standards-compliant + * implementation. Check for this. + */ +#if !defined(__USE_MINGW_ANSI_STDIO) +#error "Must use MinGW stdio for snprintf/vsnprintf" +#endif + #define NOIMPL(name,...) static inline int name(__VA_ARGS__) { errno = ENOSYS; return -1; } #define IMPL(name,ret,retval,...) static inline ret name(__VA_ARGS__) { return retval; } diff --git a/libbb/platform.c b/libbb/platform.c index 3cb41347f..2bf34f5bc 100644 --- a/libbb/platform.c +++ b/libbb/platform.c @@ -51,21 +51,8 @@ int dprintf(int fd, const char *format, ...) char *string_ptr; va_start(p, format); - if (ENABLE_PLATFORM_MINGW32) { - string_ptr = xmalloc(1024); - r = vsnprintf(string_ptr, 1024, format, p); - } - else - r = vasprintf(&string_ptr, format, p); + r = vasprintf(&string_ptr, format, p); va_end(p); - if (ENABLE_PLATFORM_MINGW32 && r > 0) { - free(string_ptr); - r += 2; - string_ptr = xmalloc(r); - va_start(p, format); - r = vsnprintf(string_ptr, r, format, p); - va_end(p); - } if (r >= 0) { r = full_write(fd, string_ptr, r); free(string_ptr); diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index 70d3fc96c..d8a42ba0b 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -295,22 +295,8 @@ char* FAST_FUNC xasprintf(const char *format, ...) char *string_ptr; va_start(p, format); -#if ENABLE_PLATFORM_MINGW32 - string_ptr = xmalloc(1024); - r = vsnprintf(string_ptr, 1024, format, p); - va_end(p); - if (r > 0) { - free(string_ptr); - r += 2; - string_ptr = xmalloc(r); - va_start(p, format); - r = vsnprintf(string_ptr, r, format, p); - va_end(p); - } -#else r = vasprintf(&string_ptr, format, p); va_end(p); -#endif if (r < 0) bb_error_msg_and_die(bb_msg_memory_exhausted); -- cgit v1.2.3-55-g6feb