msvcrt: strncpy doesn't compliant C standard

Álvaro Nieto alvaro.nieto at
Tue Aug 6 02:09:20 CDT 2013

This patch solves [Bug 34211]. The implementation of strncpy function 
doesn't compliant with C standard [1]. Also Microsoft Visual Studio 
C/C++ compiler is ok with the standard [2].

Extract from msdn;

"The strncpy function copies the initial count characters of strSource 
to strDest and returns strDest. If count is less than or equal to the 
length of strSource, a null character is not appended automatically to 
the copied string. If count is greater than the length of strSource, the 
destination string is padded with null characters up to length count. 
The behavior of strncpy is undefined if the source and destination 
strings overlap."


-------------- next part --------------
diff --git a/dlls/msvcrt/string.c b/dlls/msvcrt/string.c
index c5d0e18..8a9f7b5 100644
--- a/dlls/msvcrt/string.c
+++ b/dlls/msvcrt/string.c
@@ -576,10 +576,18 @@ int CDECL MSVCRT__strnicoll( const char* str1, const char* str2, MSVCRT_size_t c
 char* __cdecl MSVCRT_strncpy(char *dst, const char *src, MSVCRT_size_t len)
-    MSVCRT_size_t i;
-    for(i=0; i<len; i++)
-        if((dst[i] = src[i]) == '\0') break;
+    MSVCRT_size_t i, src_len;
+    src_len = strlen(src);
+    for(i=0; i<src_len; i++)
+        dst[i] = src[i];
+    /* C Standard */
+    /* If the array pointed to by s2 is a string that is shorter than n bytes,
+     * null bytes shall be appended to the copy in the array pointed to by s1,
+     * until n bytes in all are written */
+    for(i=src_len+1; i<len; i++)
+        dst[i] = '\0';
     return dst;

More information about the wine-patches mailing list