[Spice-devel] [PATCH v2] Fix swprintf_s call under MingW

Frediano Ziglio fziglio at redhat.com
Wed Aug 10 09:19:03 UTC 2016


For some reason this call does not behave correctly.
This was causing vdservice to fail to run the agent if compiled
with MingW.
Provide a proper swprintf_s replacement.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 common/vdcommon.cpp | 16 ++++++++++++++++
 common/vdcommon.h   |  8 ++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

Changes from v1:
- do not use swprintf_s using old msvcrt as not available
  under Windows XP but provide a replacement function.

diff --git a/common/vdcommon.cpp b/common/vdcommon.cpp
index 4f80a2c..f5311e3 100644
--- a/common/vdcommon.cpp
+++ b/common/vdcommon.cpp
@@ -15,6 +15,8 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <stdarg.h>
+
 #include "vdcommon.h"
 
 int supported_system_version()
@@ -76,3 +78,17 @@ errno_t vdagent_strcpy_s(char *strDestination,
     return 0;
 }
 #endif
+
+#ifndef HAVE_SWPRINTF_S
+int vdagent_swprintf_s(wchar_t *buf, size_t len, const wchar_t *format, ...)
+{
+    va_list ap;
+    va_start(ap, format);
+    int res = _vsnwprintf(buf, len, format, ap);
+    va_end(ap);
+    if ((res < 0 || (unsigned) res >= len) && len > 0) {
+        buf[0] = 0;
+    }
+    return res;
+}
+#endif
diff --git a/common/vdcommon.h b/common/vdcommon.h
index 93bb673..c5f6e93 100644
--- a/common/vdcommon.h
+++ b/common/vdcommon.h
@@ -62,8 +62,6 @@ typedef CRITICAL_SECTION mutex_t;
  * those functions are not required to be in that dll on the guest.
  */
 #ifdef OLDMSVCRT
-#define swprintf_s(buf, sz, format...) swprintf(buf, format)
-
 #ifndef _ftime_s
 #define _ftime_s(timeb) _ftime(timeb)
 #endif
@@ -72,6 +70,7 @@ typedef CRITICAL_SECTION mutex_t;
 #ifdef _MSC_VER // compiling with Visual Studio
 #define HAVE_STRCAT_S 1
 #define HAVE_STRCPY_S 1
+#define HAVE_SWPRINTF_S 1
 #endif
 
 #ifdef HAVE_STRCAT_S
@@ -90,6 +89,11 @@ errno_t vdagent_strcpy_s(char *strDestination,
                          const char *strSource);
 #endif
 
+#ifndef HAVE_SWPRINTF_S
+int vdagent_swprintf_s(wchar_t *buf, size_t len, const wchar_t *format, ...);
+#define swprintf_s vdagent_swprintf_s
+#endif
+
 #ifdef _MSC_VER // compiling with Visual Studio
 #define snprintf         sprintf_s
 #define sscanf           sscanf_s
-- 
2.7.4



More information about the Spice-devel mailing list