Handle invalid buffer pointers when count is zero.
[glibc/history.git] / wcsmbs / wcsncpy.c
blob06a20d2333fd5139731a2e32ae777c7217b82c33
1 /* Copyright (C) 1995, 1996, 1997, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <wchar.h>
23 /* Copy no more than N wide-characters of SRC to DEST. */
24 wchar_t *
25 __wcsncpy (dest, src, n)
26 wchar_t *dest;
27 const wchar_t *src;
28 size_t n;
30 wint_t c;
31 wchar_t *const s = dest;
33 --dest;
35 if (n >= 4)
37 size_t n4 = n >> 2;
39 for (;;)
41 c = *src++;
42 *++dest = c;
43 if (c == L'\0')
44 break;
45 c = *src++;
46 *++dest = c;
47 if (c == L'\0')
48 break;
49 c = *src++;
50 *++dest = c;
51 if (c == L'\0')
52 break;
53 c = *src++;
54 *++dest = c;
55 if (c == L'\0')
56 break;
57 if (--n4 == 0)
58 goto last_chars;
60 n = n - (dest - s) - 1;
61 if (n == 0)
62 return s;
63 goto zero_fill;
66 last_chars:
67 n &= 3;
68 if (n == 0)
69 return s;
73 c = *src++;
74 *++dest = c;
75 if (--n == 0)
76 return s;
78 while (c != L'\0');
80 zero_fill:
82 *++dest = L'\0';
83 while (--n > 0);
85 return s;
87 weak_alias (__wcsncpy, wcsncpy)