From 95d04aa1134b3bcbf79d2c8aa4102ec486c59b80 Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Mon, 2 Feb 2015 14:35:22 +0100 Subject: Add strcat_s/strcpy_s fallbacks These security functions are available when building with MSVC++. With mingw, they can be used at build time, but their availability will depend on the version of MSVCRT the user has installed on their system. In particular, a default install of Windows XP will not have a new enough MSVCRT version, causing runtime failures as the binary built with mingw and using strcat_s will not be able to find the necessary entry point in the MSVCRT runtime. This commit adds some strcat_s/strcpy_s-like functions used with mingw which will always be available. --- common/vdcommon.cpp | 36 ++++++++++++++++++++++++++++++++++++ common/vdcommon.h | 21 +++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/common/vdcommon.cpp b/common/vdcommon.cpp index 4dc50b4..40c9e13 100644 --- a/common/vdcommon.cpp +++ b/common/vdcommon.cpp @@ -34,3 +34,39 @@ int supported_system_version() } return 0; } + +#ifndef HAVE_STRCAT_S +errno_t vdagent_strcat_s(char *strDestination, + size_t numberOfElements, + const char *strSource) +{ + if (strDestination == NULL) + return EINVAL; + strDestination[0] = '\0'; + if (strSource == NULL) + return EINVAL; + if (strlen(strDestination) + strlen(strSource) + 1 > numberOfElements) { + return ERANGE; + } + + return strcat(strDestination, strSource); +} +#endif + +#ifndef HAVE_STRCPY_S +errno_t vdagent_strcpy_s(char *strDestination, + size_t numberOfElements, + const char *strSource) +{ + if (strDestination == NULL) + return EINVAL; + strDestination[0] = '\0'; + if (strSource == NULL) + return EINVAL; + if (strlen(strSource) + 1 > numberOfElements) { + return ERANGE; + } + + return strcpy(strDestination, strSource); +} +#endif diff --git a/common/vdcommon.h b/common/vdcommon.h index af270db..01bbbc8 100644 --- a/common/vdcommon.h +++ b/common/vdcommon.h @@ -68,6 +68,27 @@ typedef CRITICAL_SECTION mutex_t; #endif #endif /* OLDMSVCRT */ +#ifdef _MSC_VER // compiling with Visual Studio +#define HAVE_STRCAT_S 1 +#define HAVE_STRCPY_S 1 +#endif + +#ifdef HAVE_STRCAT_S +#define vdagent_strcat_s +#else +errno_t vdagent_strcat_s(char *strDestination, + size_t numberOfElements, + const char *strSource) +#endif + +#ifdef HAVE_STRCPY_S +#define vdagent_strcpy_s +#else +errno_t vdagent_strcpy_s(char *strDestination, + size_t numberOfElements, + const char *strSource) +#endif + #ifdef _MSC_VER // compiling with Visual Studio #define snprintf sprintf_s #define strncpy(d,s,n) strcpy_s(s, __min(n+1, sizeof(d)), s) -- cgit v1.2.3