[Spice-devel,vdagent-win,1/2] vdagent: file-xfer: fix compilation warnings when building with Visual Studio

Submitted by Uri Lublin on March 9, 2014, 5:05 p.m.

Details

Message ID 1394384706-29711-2-git-send-email-uril@redhat.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Uri Lublin March 9, 2014, 5:05 p.m.
This patch reverts some of the changes made in commit
4b9e9b1d28ea7eaec44ff73e2f91c4064986b12a
---
 vdagent/file_xfer.cpp | 23 +++++++++++++++++------
 vdagent/file_xfer.h   |  2 +-
 2 files changed, 18 insertions(+), 7 deletions(-)

Patch hide | download patch | download mbox

diff --git a/vdagent/file_xfer.cpp b/vdagent/file_xfer.cpp
index e402eb2..cdb5654 100644
--- a/vdagent/file_xfer.cpp
+++ b/vdagent/file_xfer.cpp
@@ -18,7 +18,18 @@ 
 #include <shlobj.h>
 #define __STDC_FORMAT_MACROS
 #define __USE_MINGW_ANSI_STDIO 1
+
+// compiler specific definitions
+#ifdef _MSC_VER // compiling with Visual Studio
+#define PRIu64 "I64u"
+#else // compiling with mingw
 #include <inttypes.h>
+#define sscanf_s sscanf
+//#define sprintf_s snprintf /* apprently sprintf_s is implemented in mingw */
+#define strcpy_s(d, n, s) strncpy(d, s, n)
+#define strcat_s(d, n, s) strncat(d, s, n)
+#endif // compiler specific definitions
+
 #include <stdio.h>
 #include "file_xfer.h"
 #include "as_user.h"
@@ -75,12 +86,12 @@  void FileXfer::handle_start(VDAgentFileXferStartMessage* start,
     }

     if (strlen(file_path) + strlen(file_name) + 1 >= MAX_PATH) {
-        vd_printf("error: file too long %s\%s", file_path, file_name);
+        vd_printf("error: file too long %s\\%s", file_path, file_name);
         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());
@@ -188,10 +199,10 @@  bool FileXfer::g_key_get_string(char* data, const char* group, const char* key,
     char *group_pos, *key_pos, *next_group_pos, *start, *end;
     unsigned len;

-    snprintf(group_pfx, sizeof(group_pfx), "[%s]", group);
+    sprintf_s(group_pfx, sizeof(group_pfx), "[%s]", group);
     if (!(group_pos = strstr((char*)data, group_pfx))) return false;

-    snprintf(key_pfx, sizeof(key_pfx), "\n%s=", key);
+    sprintf_s(key_pfx, sizeof(key_pfx), "\n%s=", key);
     if (!(key_pos = strstr(group_pos, key_pfx))) return false;

     next_group_pos = strstr(group_pos + strlen(group_pfx), "[");
@@ -216,5 +227,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);
 }
diff --git a/vdagent/file_xfer.h b/vdagent/file_xfer.h
index b506f59..2ffc320 100644
--- a/vdagent/file_xfer.h
+++ b/vdagent/file_xfer.h
@@ -25,7 +25,7 @@  typedef struct ALIGN_VC FileXferTask {
     FileXferTask(HANDLE _handle, uint64_t _size, char* _name):
     handle(_handle), size(_size), pos(0) {
         // FIXME: should raise an error if name is too long..
-        strncpy(name, _name, sizeof(name) - 1);
+        strcpy_s(name, sizeof(name) -1, _name);
     }
     HANDLE handle;
     uint64_t size;

Comments

What is the compilation warning?
I guess you could mention in the log that this readds uses of _s methods,
but substitute them with non _s methods on mingw through #define

On Sun, Mar 09, 2014 at 07:05:05PM +0200, Uri Lublin wrote:
> This patch reverts some of the changes made in commit
> 4b9e9b1d28ea7eaec44ff73e2f91c4064986b12a
> ---
>  vdagent/file_xfer.cpp | 23 +++++++++++++++++------
>  vdagent/file_xfer.h   |  2 +-
>  2 files changed, 18 insertions(+), 7 deletions(-)
> 
> diff --git a/vdagent/file_xfer.cpp b/vdagent/file_xfer.cpp
> index e402eb2..cdb5654 100644
> --- a/vdagent/file_xfer.cpp
> +++ b/vdagent/file_xfer.cpp
> @@ -18,7 +18,18 @@
>  #include <shlobj.h>
>  #define __STDC_FORMAT_MACROS
>  #define __USE_MINGW_ANSI_STDIO 1
> +
> +// compiler specific definitions
> +#ifdef _MSC_VER // compiling with Visual Studio
> +#define PRIu64 "I64u"
> +#else // compiling with mingw
>  #include <inttypes.h>
> +#define sscanf_s sscanf
> +//#define sprintf_s snprintf /* apprently sprintf_s is implemented in mingw */
> +#define strcpy_s(d, n, s) strncpy(d, s, n)
> +#define strcat_s(d, n, s) strncat(d, s, n)
> +#endif // compiler specific definitions
> +
>  #include <stdio.h>
>  #include "file_xfer.h"
>  #include "as_user.h"
> @@ -75,12 +86,12 @@ void FileXfer::handle_start(VDAgentFileXferStartMessage* start,
>      }
> 
>      if (strlen(file_path) + strlen(file_name) + 1 >= MAX_PATH) {
> -        vd_printf("error: file too long %s\%s", file_path, file_name);
> +        vd_printf("error: file too long %s\\%s", file_path, file_name);

Can you split this in a separate commit?

Thanks,

Christophe