msvcrt: strncpy doesn't compliant C standard (try 3)

Álvaro Nieto alvaro.nieto at gmail.com
Wed Aug 7 15:15:43 CDT 2013


Add tests for array of chars.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.winehq.org/pipermail/wine-patches/attachments/20130807/39ac52b9/attachment.html>
-------------- next part --------------
diff --git a/dlls/msvcrt/string.c b/dlls/msvcrt/string.c
index c5d0e18..96ee062 100644
--- a/dlls/msvcrt/string.c
+++ b/dlls/msvcrt/string.c
@@ -581,6 +581,13 @@ char* __cdecl MSVCRT_strncpy(char *dst, const char *src, MSVCRT_size_t len)
     for(i=0; i<len; i++)
         if((dst[i] = src[i]) == '\0') break;
 
+    /* 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<len; i++)
+        dst[i] = '\0';
+
     return dst;
 }
 
diff --git a/dlls/msvcrt/tests/string.c b/dlls/msvcrt/tests/string.c
index 7edaa23..6aead92 100644
--- a/dlls/msvcrt/tests/string.c
+++ b/dlls/msvcrt/tests/string.c
@@ -2554,6 +2554,43 @@ static void test_atoi(void)
     ok(r == 0, "atoi(4294967296) = %d\n", r);
 }
 
+static void test_strncpy(void)
+{
+    size_t len = 10;
+    char *ret;
+    char dst[len + 1];
+    char not_null_high[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
+    char not_null_less[] = {'0', '1', '2', '3', '4', '5'};
+
+    /* strlen(src) > len */
+    ret = strncpy(dst, "01234567890123456789", len);
+    ok(ret == dst, "ret != dst\n");
+    ok(!strncmp(dst, "0123456789", len), "dst != 0123456789\n");
+
+    /* without null-terminated */
+    ret = strncpy(dst, not_null_high, len);
+    ok(ret == dst, "ret != dst\n");
+    ok(!strncmp(dst, "0123456789", len), "dst != 0123456789\n");
+
+    /* strlen(src) < len */
+    strcpy(dst, "0123456789");
+    ret = strncpy(dst, "012345", len);
+    ok(ret == dst, "ret != dst\n");
+    ok(!strcmp(dst, "012345"), "dst != 012345\n");
+    ok(dst[len - 1] == '\0', "dst[len - 1] != 0\n");
+
+    /* without null-terminated */
+    ret = strncpy(dst, not_null_less, len);
+    ok(ret == dst, "ret != dst\n");
+    ok(!strcmp(dst, "012345"), "dst != 012345\n");
+    ok(dst[len - 1] == '\0', "dst[len - 1] != 0\n");
+
+    /* strlen(src) == len */
+    ret = strncpy(dst, "0123456789", len);
+    ok(ret == dst, "ret != dst\n");
+    ok(!strncmp(dst, "0123456789", len), "dst != 0123456789\n");
+}
+
 START_TEST(string)
 {
     char mem[100];
@@ -2655,4 +2692,5 @@ START_TEST(string)
     test__stricmp();
     test__wcstoi64();
     test_atoi();
+    test_strncpy();
 }


More information about the wine-patches mailing list