From 312d08b290d079f5d72d9afc14a47606ebdde240 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 26 Aug 2016 10:03:05 +0200 Subject: qstrncpy: don't call strncpy_s with invalid parameters According to https://msdn.microsoft.com/en-us/library/5dae5d43.aspx, strncpy_s' second argument must not be 0: > If strDest or strSource is NULL, *or numberOfElements is 0*, the > invalid parameter handler is invoked. Move the existing check for len > 0 up to protect the strncpy_s call, too. Change-Id: I70d339ea60d4b76f3038b2e4e4756f6590a9bd31 Reviewed-by: Edward Welbourne Reviewed-by: Thiago Macieira --- src/corelib/tools/qbytearray.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 00d15fd518..8bae505d76 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -208,13 +208,14 @@ char *qstrncpy(char *dst, const char *src, uint len) { if (!src || !dst) return 0; + if (len > 0) { #if defined(_MSC_VER) && _MSC_VER >= 1400 - strncpy_s(dst, len, src, len-1); + strncpy_s(dst, len, src, len - 1); #else - strncpy(dst, src, len); + strncpy(dst, src, len); #endif - if (len > 0) dst[len-1] = '\0'; + } return dst; } -- cgit v1.2.3