2.5-18.1
[glibc.git] / debug / strncat_chk.c
blob953b435a4bd452b1bd7493cfb62bc1790702f226
1 /* Copyright (C) 1991, 1997, 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <string.h>
21 #include <memcopy.h>
24 char *
25 __strncat_chk (s1, s2, n, s1len)
26 char *s1;
27 const char *s2;
28 size_t n;
29 size_t s1len;
31 reg_char c;
32 char *s = s1;
34 /* Find the end of S1. */
37 if (__builtin_expect (s1len-- == 0, 0))
38 __chk_fail ();
39 c = *s1++;
41 while (c != '\0');
43 /* Make S1 point before next character, so we can increment
44 it while memory is read (wins on pipelined cpus). */
45 ++s1len;
46 s1 -= 2;
48 if (n >= 4)
50 size_t n4 = n >> 2;
53 if (__builtin_expect (s1len-- == 0, 0))
54 __chk_fail ();
55 c = *s2++;
56 *++s1 = c;
57 if (c == '\0')
58 return s;
59 if (__builtin_expect (s1len-- == 0, 0))
60 __chk_fail ();
61 c = *s2++;
62 *++s1 = c;
63 if (c == '\0')
64 return s;
65 if (__builtin_expect (s1len-- == 0, 0))
66 __chk_fail ();
67 c = *s2++;
68 *++s1 = c;
69 if (c == '\0')
70 return s;
71 if (__builtin_expect (s1len-- == 0, 0))
72 __chk_fail ();
73 c = *s2++;
74 *++s1 = c;
75 if (c == '\0')
76 return s;
77 } while (--n4 > 0);
78 n &= 3;
81 while (n > 0)
83 if (__builtin_expect (s1len-- == 0, 0))
84 __chk_fail ();
85 c = *s2++;
86 *++s1 = c;
87 if (c == '\0')
88 return s;
89 n--;
92 if (c != '\0')
94 if (__builtin_expect (s1len-- == 0, 0))
95 __chk_fail ();
96 *++s1 = '\0';
99 return s;