[Spice-devel] [vdagent-win V2 4/6] Fix Visual Studio compiler warning (sscanf/strcat)

Uri Lublin uril at redhat.com
Mon Mar 24 10:02:41 PDT 2014


This commit partially reverts 4b9e9b1d28ea7eaec44ff73e2f91c4064986b12a.

Visual Studio complains about non-safe sscanf and strcat:
    vdagent\file_xfer.cpp(89) : warning C4996: 'strcat': This function
    or variable may be unsafe.  Consider using strcat_s instead.
    To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
    See online help for details.

Replace them with sscanf_s and strcat_s.
Add #define macros when building with mingw.
Note that for strcat_s, size parameter (NumberOfElements) represents the
size of destination-buffer, while for strncat, size parameter (n)
represents the number of characters to copy.
---
 vdagent/file_xfer.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/vdagent/file_xfer.cpp b/vdagent/file_xfer.cpp
index 01072ba..3bed1b1 100644
--- a/vdagent/file_xfer.cpp
+++ b/vdagent/file_xfer.cpp
@@ -24,6 +24,8 @@
 #define PRIu64 "I64u"
 #else // compiling with mingw
 #include <inttypes.h>
+#define sscanf_s sscanf
+#define strcat_s(d, n, s) strncat(d, s, n - strlen(d))
 #endif // compiler specific definitions

 #include <stdio.h>
@@ -86,8 +88,8 @@ void FileXfer::handle_start(VDAgentFileXferStartMessage* start,
         return;
     }

-    strcat(file_path, "\\");
-    strcat(file_path, file_name);
+    strcat_s(file_path, MAX_PATH, "\\");
+    strcat_s(file_path, MAX_PATH, file_name);
     handle = CreateFileA(file_path, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
     if (handle == INVALID_HANDLE_VALUE) {
         vd_printf("failed creating %s %lu", file_path, GetLastError());
@@ -223,5 +225,5 @@ bool FileXfer::g_key_get_uint64(char* data, const char* group, const char* key,

     if (!g_key_get_string(data, group, key, str, sizeof(str)))
         return false;
-    return !!sscanf(str, "%" PRIu64, value);
+    return !!sscanf_s(str, "%" PRIu64, value);
 }
-- 
1.8.5.3



More information about the Spice-devel mailing list