]> git.ipfire.org Git - thirdparty/glibc.git/blob - debug/strncat_chk.c
2.5-18.1
[thirdparty/glibc.git] / debug / strncat_chk.c
1 /* Copyright (C) 1991, 1997, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
18
19 #include <string.h>
20
21 #include <memcopy.h>
22
23
24 char *
25 __strncat_chk (s1, s2, n, s1len)
26 char *s1;
27 const char *s2;
28 size_t n;
29 size_t s1len;
30 {
31 reg_char c;
32 char *s = s1;
33
34 /* Find the end of S1. */
35 do
36 {
37 if (__builtin_expect (s1len-- == 0, 0))
38 __chk_fail ();
39 c = *s1++;
40 }
41 while (c != '\0');
42
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;
47
48 if (n >= 4)
49 {
50 size_t n4 = n >> 2;
51 do
52 {
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;
79 }
80
81 while (n > 0)
82 {
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--;
90 }
91
92 if (c != '\0')
93 {
94 if (__builtin_expect (s1len-- == 0, 0))
95 __chk_fail ();
96 *++s1 = '\0';
97 }
98
99 return s;
100 }