diff options
author | Christophe Fergeau <cfergeau@redhat.com> | 2015-02-02 14:35:22 +0100 |
---|---|---|
committer | Christophe Fergeau <cfergeau@redhat.com> | 2015-02-06 13:35:44 +0100 |
commit | 95d04aa1134b3bcbf79d2c8aa4102ec486c59b80 (patch) | |
tree | 3283f14971a91762e50f304595cba2b12e3e30bf | |
parent | ff7dc22e27aca54933eaec161a5ccd9225b70b7c (diff) |
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.
-rw-r--r-- | common/vdcommon.cpp | 36 | ||||
-rw-r--r-- | common/vdcommon.h | 21 |
2 files changed, 57 insertions, 0 deletions
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 @@ -69,6 +69,27 @@ typedef CRITICAL_SECTION mutex_t; #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) #define strcat(d,s) strcat_s(d, sizeof(d), s) |