1*f02514dfSJohn Marino /* Checking strncat.
2*f02514dfSJohn Marino Copyright (C) 1991, 1997, 2003, 2004, 2005, 2009 Free Software Foundation, Inc.
3*f02514dfSJohn Marino
4*f02514dfSJohn Marino This file is part of GCC.
5*f02514dfSJohn Marino
6*f02514dfSJohn Marino GCC is free software; you can redistribute it and/or modify it under
7*f02514dfSJohn Marino the terms of the GNU General Public License as published by the Free
8*f02514dfSJohn Marino Software Foundation; either version 3, or (at your option) any later
9*f02514dfSJohn Marino version.
10*f02514dfSJohn Marino
11*f02514dfSJohn Marino In addition to the permissions in the GNU General Public License, the
12*f02514dfSJohn Marino Free Software Foundation gives you unlimited permission to link the
13*f02514dfSJohn Marino compiled version of this file into combinations with other programs,
14*f02514dfSJohn Marino and to distribute those combinations without any restriction coming
15*f02514dfSJohn Marino from the use of this file. (The General Public License restrictions
16*f02514dfSJohn Marino do apply in other respects; for example, they cover modification of
17*f02514dfSJohn Marino the file, and distribution when not linked into a combine
18*f02514dfSJohn Marino executable.)
19*f02514dfSJohn Marino
20*f02514dfSJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
21*f02514dfSJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
22*f02514dfSJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23*f02514dfSJohn Marino for more details.
24*f02514dfSJohn Marino
25*f02514dfSJohn Marino Under Section 7 of GPL version 3, you are granted additional
26*f02514dfSJohn Marino permissions described in the GCC Runtime Library Exception, version
27*f02514dfSJohn Marino 3.1, as published by the Free Software Foundation.
28*f02514dfSJohn Marino
29*f02514dfSJohn Marino You should have received a copy of the GNU General Public License and
30*f02514dfSJohn Marino a copy of the GCC Runtime Library Exception along with this program;
31*f02514dfSJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
32*f02514dfSJohn Marino <http://www.gnu.org/licenses/>. */
33*f02514dfSJohn Marino
34*f02514dfSJohn Marino
35*f02514dfSJohn Marino #include "config.h"
36*f02514dfSJohn Marino #include <ssp/ssp.h>
37*f02514dfSJohn Marino #ifdef HAVE_STRING_H
38*f02514dfSJohn Marino # include <string.h>
39*f02514dfSJohn Marino #endif
40*f02514dfSJohn Marino
41*f02514dfSJohn Marino extern void __chk_fail (void) __attribute__((__noreturn__));
42*f02514dfSJohn Marino
43*f02514dfSJohn Marino char *
__strncat_chk(char * __restrict__ dest,const char * __restrict__ src,size_t n,size_t slen)44*f02514dfSJohn Marino __strncat_chk (char *__restrict__ dest, const char *__restrict__ src,
45*f02514dfSJohn Marino size_t n, size_t slen)
46*f02514dfSJohn Marino {
47*f02514dfSJohn Marino char c;
48*f02514dfSJohn Marino char *s = dest;
49*f02514dfSJohn Marino
50*f02514dfSJohn Marino do
51*f02514dfSJohn Marino {
52*f02514dfSJohn Marino if (slen-- == 0)
53*f02514dfSJohn Marino __chk_fail ();
54*f02514dfSJohn Marino c = *dest++;
55*f02514dfSJohn Marino }
56*f02514dfSJohn Marino while (c != '\0');
57*f02514dfSJohn Marino
58*f02514dfSJohn Marino ++slen;
59*f02514dfSJohn Marino dest -= 2;
60*f02514dfSJohn Marino
61*f02514dfSJohn Marino if (n >= 4)
62*f02514dfSJohn Marino {
63*f02514dfSJohn Marino size_t n4 = n >> 2;
64*f02514dfSJohn Marino do
65*f02514dfSJohn Marino {
66*f02514dfSJohn Marino if (slen-- == 0)
67*f02514dfSJohn Marino __chk_fail ();
68*f02514dfSJohn Marino c = *src++;
69*f02514dfSJohn Marino *++dest = c;
70*f02514dfSJohn Marino if (c == '\0')
71*f02514dfSJohn Marino return s;
72*f02514dfSJohn Marino if (slen-- == 0)
73*f02514dfSJohn Marino __chk_fail ();
74*f02514dfSJohn Marino c = *src++;
75*f02514dfSJohn Marino *++dest = c;
76*f02514dfSJohn Marino if (c == '\0')
77*f02514dfSJohn Marino return s;
78*f02514dfSJohn Marino if (slen-- == 0)
79*f02514dfSJohn Marino __chk_fail ();
80*f02514dfSJohn Marino c = *src++;
81*f02514dfSJohn Marino *++dest = c;
82*f02514dfSJohn Marino if (c == '\0')
83*f02514dfSJohn Marino return s;
84*f02514dfSJohn Marino if (slen-- == 0)
85*f02514dfSJohn Marino __chk_fail ();
86*f02514dfSJohn Marino c = *src++;
87*f02514dfSJohn Marino *++dest = c;
88*f02514dfSJohn Marino if (c == '\0')
89*f02514dfSJohn Marino return s;
90*f02514dfSJohn Marino if (slen-- == 0)
91*f02514dfSJohn Marino __chk_fail ();
92*f02514dfSJohn Marino c = *src++;
93*f02514dfSJohn Marino *++dest = c;
94*f02514dfSJohn Marino if (c == '\0')
95*f02514dfSJohn Marino return s;
96*f02514dfSJohn Marino } while (--n4 > 0);
97*f02514dfSJohn Marino n &= 3;
98*f02514dfSJohn Marino }
99*f02514dfSJohn Marino
100*f02514dfSJohn Marino while (n > 0)
101*f02514dfSJohn Marino {
102*f02514dfSJohn Marino if (slen-- == 0)
103*f02514dfSJohn Marino __chk_fail ();
104*f02514dfSJohn Marino c = *src++;
105*f02514dfSJohn Marino *++dest = c;
106*f02514dfSJohn Marino if (c == '\0')
107*f02514dfSJohn Marino return s;
108*f02514dfSJohn Marino n--;
109*f02514dfSJohn Marino }
110*f02514dfSJohn Marino
111*f02514dfSJohn Marino if (c != '\0')
112*f02514dfSJohn Marino {
113*f02514dfSJohn Marino if (slen-- == 0)
114*f02514dfSJohn Marino __chk_fail ();
115*f02514dfSJohn Marino *++dest = '\0';
116*f02514dfSJohn Marino }
117*f02514dfSJohn Marino
118*f02514dfSJohn Marino return s;
119*f02514dfSJohn Marino }
120