[Spice-devel] [vdagent-win 1/3] Add strcat_s/strcpy_s fallbacks

Christophe Fergeau cfergeau at redhat.com
Mon Feb 16 02:28:58 PST 2015


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 | 42 ++++++++++++++++++++++++++++++++++++++++++
 common/vdcommon.h   | 22 ++++++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/common/vdcommon.cpp b/common/vdcommon.cpp
index 4dc50b4..4f80a2c 100644
--- a/common/vdcommon.cpp
+++ b/common/vdcommon.cpp
@@ -34,3 +34,45 @@ 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;
+    if (strSource == NULL) {
+        strDestination[0] = '\0';
+        return EINVAL;
+    }
+    if (strlen(strDestination) + strlen(strSource) + 1 > numberOfElements) {
+        strDestination[0] = '\0';
+        return ERANGE;
+    }
+
+    strcat(strDestination, strSource);
+
+    return 0;
+}
+#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;
+    }
+
+    strcpy(strDestination, strSource);
+
+    return 0;
+}
+#endif
diff --git a/common/vdcommon.h b/common/vdcommon.h
index af270db..002ac23 100644
--- a/common/vdcommon.h
+++ b/common/vdcommon.h
@@ -22,6 +22,7 @@
 #pragma warning(disable:4200)
 #endif
 
+#include <errno.h>
 #include <windows.h>
 #include "spice/vd_agent.h"
 #include "vdlog.h"
@@ -69,6 +70,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 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 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)
-- 
2.1.0



More information about the Spice-devel mailing list