elf: Do not parse ill-formatted strings
[glibc.git] / wcsmbs / tst-wcslcpy.c
blob8eaffbf0c401ffbf46943379875792467a29d56f
1 /* Test the wcslcpy function.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <support/check.h>
23 #include <wchar.h>
25 static int
26 do_test (void)
28 struct {
29 wchar_t buf1[16];
30 wchar_t buf2[16];
31 } s;
33 /* Nothing is written to the destination if its size is 0. */
34 wmemset (s.buf1, '@', array_length (s.buf1));
35 wmemset (s.buf2, '@', array_length (s.buf2));
36 TEST_COMPARE (wcslcpy (s.buf1, L"Hello!", 0), 6);
37 TEST_COMPARE_BLOB (&s, sizeof (s), L"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
39 /* No bytes are are modified in the target buffer if the source
40 string is short enough. */
41 wmemset (s.buf1, '@', array_length (s.buf1));
42 wmemset (s.buf2, '@', array_length (s.buf2));
43 TEST_COMPARE (wcslcpy (s.buf1, L"Hello!", array_length (s.buf1)), 6);
44 TEST_COMPARE_BLOB (&s, sizeof (s),
45 L"Hello!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
47 /* A source string which fits exactly into the destination buffer is
48 not truncated. */
49 wmemset (s.buf1, '@', array_length (s.buf1));
50 wmemset (s.buf2, '@', array_length (s.buf2));
51 TEST_COMPARE (wcslcpy (s.buf1, L"Hello, world!!!", array_length (s.buf1)),
52 15);
53 TEST_COMPARE_BLOB (&s, sizeof (s),
54 L"Hello, world!!!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
56 /* A source string one character longer than the destination buffer
57 is truncated by one character. The untruncated source length is
58 returned. */
59 wmemset (s.buf1, '@', array_length (s.buf1));
60 wmemset (s.buf2, '@', array_length (s.buf2));
61 TEST_COMPARE (wcslcpy (s.buf1, L"Hello, world!!!!", array_length (s.buf1)),
62 16);
63 TEST_COMPARE_BLOB (&s, sizeof (s),
64 L"Hello, world!!!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
66 /* An even longer source string is truncated as well, and the
67 original length is returned. */
68 wmemset (s.buf1, '@', array_length (s.buf1));
69 wmemset (s.buf2, '@', array_length (s.buf2));
70 TEST_COMPARE (wcslcpy (s.buf1, L"Hello, world!!!!!!!!",
71 array_length (s.buf1)), 20);
72 TEST_COMPARE_BLOB (&s, sizeof (s),
73 L"Hello, world!!!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 128);
75 return 0;
78 #include <support/test-driver.c>