[Spice-devel] [vdagent-win PATCH V3 09/11] Fix Visual Studio compiler warning (strncpy)

Uri Lublin uril at redhat.com
Tue Dec 30 08:20:55 PST 2014


Visual Studio complains:
  vdagent\file_xfer.h(28) : warning C4996: 'strncpy': This function or variable may
  Consider using strncpy_s instead.
  To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
  See online help for details.

Replace strncpy with strcpy_s when building with Visual Studio.
Adjust parameter order and difference in meaning of length:
- length param is last in strncpy and second in strcpy_s
- many calls to strncpy are given length param n-1, as
  strncpy does not guarantee that destination ends with '\0'
- it's safer to set length param not larger than sizeof(d)
  (assuming d is an array not a pointer)

Trying to use strcpy_s for both Visual Studio and mingw
failed as strcpy_s is missing from the default msvcrt.dll
on WinXP (See OLDMSVCRT in common/vdcommon.h).
---
 common/vdcommon.h   |    1 +
 vdagent/file_xfer.h |    2 ++
 2 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/common/vdcommon.h b/common/vdcommon.h
index 568e3ba..f285175 100644
--- a/common/vdcommon.h
+++ b/common/vdcommon.h
@@ -70,6 +70,7 @@ typedef CRITICAL_SECTION mutex_t;
 
 #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)
 #endif
 
 enum SystemVersion {
diff --git a/vdagent/file_xfer.h b/vdagent/file_xfer.h
index b506f59..07a6808 100644
--- a/vdagent/file_xfer.h
+++ b/vdagent/file_xfer.h
@@ -25,6 +25,8 @@ 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..
+        //        currently the only user is FileXfer::handle_start
+        //        which verifies that strlen(_name) < MAX_PATH
         strncpy(name, _name, sizeof(name) - 1);
     }
     HANDLE handle;
-- 
1.7.1



More information about the Spice-devel mailing list