miniupnpc: Fix usage of Windows _snprintf() function
_snprintf() differs from snprintf() in: * on overflow it returns -1 instead of required buffer size * on overflow it does not fill nul byte * does not accept NULL/0 as a buffer Microsoft implemented snprintf() in Visual Studio 2015 as part of UCRT. Mingw32 contains snprintf() implementation only when __USE_MINGW_ANSI_STDIO is defined. Mingw-w64 versions prior to 8.0.0. contain snprintf() implementation when __USE_MINGW_ANSI_STDIO or _UCRT is defined. Since version 8.0.0 it is always supported. Mingw-w64 defines both __MINGW32__ and __MINGW64_VERSION_MAJOR macros. Mingw32 defines only __MINGW32__. _scprintf() just count number of bytes needed for formatting string, so it is basically return value of snprintf(). This change updates miniupnpc code to use snprintf() when is provided by compiler/runtime to avoid usage _snprintf(). And also this changes updates miniupnpc emulation of snprintf() by _snprintf() and _scprintf() functions to avoid buffer overflows. For inspiration full emulation of snprintf() by _snprintf() is available in mingw-w64 stdio library: https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-crt/stdio/snprintf.c
This commit is contained in:
parent
992565201b
commit
7d5fdf0743
|
@ -19,7 +19,11 @@
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#define MAXHOSTNAMELEN 64
|
#define MAXHOSTNAMELEN 64
|
||||||
#define snprintf _snprintf
|
/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */
|
||||||
|
#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO))
|
||||||
|
/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */
|
||||||
|
#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__)))
|
||||||
|
#endif
|
||||||
#define herror
|
#define herror
|
||||||
#define socklen_t int
|
#define socklen_t int
|
||||||
#else /* #ifdef _WIN32 */
|
#else /* #ifdef _WIN32 */
|
||||||
|
|
|
@ -13,7 +13,11 @@
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#define snprintf _snprintf
|
/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */
|
||||||
|
#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO))
|
||||||
|
/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */
|
||||||
|
#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__)))
|
||||||
|
#endif
|
||||||
#else
|
#else
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
|
@ -20,7 +20,11 @@
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <iphlpapi.h>
|
#include <iphlpapi.h>
|
||||||
#define snprintf _snprintf
|
/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */
|
||||||
|
#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO))
|
||||||
|
/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */
|
||||||
|
#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__)))
|
||||||
|
#endif
|
||||||
#if !defined(_MSC_VER)
|
#if !defined(_MSC_VER)
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#else /* !defined(_MSC_VER) */
|
#else /* !defined(_MSC_VER) */
|
||||||
|
|
|
@ -15,7 +15,11 @@
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <iphlpapi.h>
|
#include <iphlpapi.h>
|
||||||
#define snprintf _snprintf
|
/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */
|
||||||
|
#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO))
|
||||||
|
/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */
|
||||||
|
#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__)))
|
||||||
|
#endif
|
||||||
#define strdup _strdup
|
#define strdup _strdup
|
||||||
#if !defined(_MSC_VER)
|
#if !defined(_MSC_VER)
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
|
@ -15,7 +15,11 @@
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#define MAXHOSTNAMELEN 64
|
#define MAXHOSTNAMELEN 64
|
||||||
#define snprintf _snprintf
|
/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */
|
||||||
|
#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO))
|
||||||
|
/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */
|
||||||
|
#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__)))
|
||||||
|
#endif
|
||||||
#define socklen_t int
|
#define socklen_t int
|
||||||
#ifndef strncasecmp
|
#ifndef strncasecmp
|
||||||
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
|
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
|
||||||
|
|
|
@ -11,7 +11,11 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#define snprintf _snprintf
|
/* snprintf is supported by Visual Studio 2015, mingw-w64 8.0.0, mingw-w64 with ucrt, mingw-w64 or mingw32 with ansi stdio */
|
||||||
|
#if (defined(_MSC_VER) && _MSC_VER < 1900) || (defined(__MINGW64_VERSION_MAJOR) && __MINGW64_VERSION_MAJOR < 8 && !defined(_UCRT) && !defined(__USE_MINGW_ANSI_STDIO)) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && !defined(__USE_MINGW_ANSI_STDIO))
|
||||||
|
/* _snprintf does not fill nul byte at the end of buffer and returns -1 on overflow */
|
||||||
|
#define snprintf(buf, size, fmt, ...) ((_snprintf((buf), (size), (fmt), __VA_ARGS__), (((char *)buf)[(size_t)(size)-1] = 0), _scprintf((fmt), __VA_ARGS__)))
|
||||||
|
#endif
|
||||||
#else
|
#else
|
||||||
/* for IPPROTO_TCP / IPPROTO_UDP */
|
/* for IPPROTO_TCP / IPPROTO_UDP */
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
Loading…
Reference in New Issue