Mention BZ #14229 in NEWS
[glibc.git] / debug / strncpy_chk.c
blobe34c1e5d67f1957f51a52475ab12fdff1221de8b
1 /* Copyright (C) 1991, 1997, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <string.h>
19 #include <memcopy.h>
22 char *
23 __strncpy_chk (s1, s2, n, s1len)
24 char *s1;
25 const char *s2;
26 size_t n;
27 size_t s1len;
29 char c;
30 char *s = s1;
32 if (__builtin_expect (s1len < n, 0))
33 __chk_fail ();
35 --s1;
37 if (n >= 4)
39 size_t n4 = n >> 2;
41 for (;;)
43 c = *s2++;
44 *++s1 = c;
45 if (c == '\0')
46 break;
47 c = *s2++;
48 *++s1 = c;
49 if (c == '\0')
50 break;
51 c = *s2++;
52 *++s1 = c;
53 if (c == '\0')
54 break;
55 c = *s2++;
56 *++s1 = c;
57 if (c == '\0')
58 break;
59 if (--n4 == 0)
60 goto last_chars;
62 n = n - (s1 - s) - 1;
63 if (n == 0)
64 return s;
65 goto zero_fill;
68 last_chars:
69 n &= 3;
70 if (n == 0)
71 return s;
75 c = *s2++;
76 *++s1 = c;
77 if (--n == 0)
78 return s;
80 while (c != '\0');
82 zero_fill:
84 *++s1 = '\0';
85 while (--n > 0);
87 return s;