From 06af237c7008086a5389871320e5669dd51a0fdc Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Sun, 17 Mar 2013 20:51:52 +1100 Subject: [PATCH] Add file src/share/grabbag/snprintf.c to grabbag library. Define function flac_snprintf() which has ISO C99 snprintf() behavior even when compiling with Microsoft Visual Studio, by wrapping the MSVS snprintf_s() function. --- include/share/compat.h | 10 +++++ src/share/grabbag/Makefile.am | 3 +- src/share/grabbag/grabbag_static.vcproj | 4 ++ src/share/grabbag/snprintf.c | 73 +++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/share/grabbag/snprintf.c diff --git a/include/share/compat.h b/include/share/compat.h index 9c3d35e..ee4c3e5 100644 --- a/include/share/compat.h +++ b/include/share/compat.h @@ -143,4 +143,14 @@ # endif #endif /* defined _MSC_VER */ + +/* FLAC needs to compile and work correctly on systems with a norrmal ISO C99 + * snprintf as well as Microsoft Visual Studio which has an non-standards + * conformant snprint_s function. + * + * This function wraps the MS version to behave more like the the ISO version. + */ + +int flac_snprintf(char *str, size_t size, const char *fmt, ...); + #endif /* FLAC__SHARE__COMPAT_H */ diff --git a/src/share/grabbag/Makefile.am b/src/share/grabbag/Makefile.am index a7f2ee7..79d211e 100644 --- a/src/share/grabbag/Makefile.am +++ b/src/share/grabbag/Makefile.am @@ -10,7 +10,8 @@ libgrabbag_la_SOURCES = \ file.c \ picture.c \ replaygain.c \ - seektable.c + seektable.c \ + snprintf.c EXTRA_DIST = \ Makefile.lite \ diff --git a/src/share/grabbag/grabbag_static.vcproj b/src/share/grabbag/grabbag_static.vcproj index a950a99..284bbb8 100644 --- a/src/share/grabbag/grabbag_static.vcproj +++ b/src/share/grabbag/grabbag_static.vcproj @@ -186,6 +186,10 @@ RelativePath=".\seektable.c" > + + +#endif + +#include +#include + +#include "share/compat.h" + +/* + * FLAC needs to compile and work correctly on systems with a norrmal ISO C99 + * snprintf as well as Microsoft Visual Studio which has an non-standards + * conformant snprint_s function. + * + * The important difference occurs when the resultant string (plus string + * terminator) would have been longer than the supplied size parameter. When + * this happens, ISO C's snprintf returns the length of resultant string, but + * does not over-write the end of the buffer. MS's snprintf_s in this case + * returns -1. + * + * The _MSC_VER code below attempts to modify the return code for snprintf_s + * to something that is more compatible with the behaviour of the ISO C version. + */ + +int +flac_snprintf(char *str, size_t size, const char *fmt, ...) +{ + va_list va; + int rc ; + + va_start (va, fmt); + +#ifdef _MSC_VER + rc = vsnprintf_s (str, size, fmt, va); + rc = (rc > 0) ? rc : (size == 0 ? 1024 : size * 2); +#else + rc = vsnprintf (str, size, fmt, va); +#endif + va_end (va); + + return rc; +} -- 2.7.4