strcat-chk.c revision 1.5.4.1 1 1.1 mrg /* Checking strcat.
2 1.5.4.1 martin Copyright (C) 1991-2017 Free Software Foundation, Inc.
3 1.1 mrg
4 1.1 mrg This file is part of GCC.
5 1.1 mrg
6 1.1 mrg GCC is free software; you can redistribute it and/or modify it under
7 1.1 mrg the terms of the GNU General Public License as published by the Free
8 1.1 mrg Software Foundation; either version 3, or (at your option) any later
9 1.1 mrg version.
10 1.1 mrg
11 1.1 mrg In addition to the permissions in the GNU General Public License, the
12 1.1 mrg Free Software Foundation gives you unlimited permission to link the
13 1.1 mrg compiled version of this file into combinations with other programs,
14 1.1 mrg and to distribute those combinations without any restriction coming
15 1.1 mrg from the use of this file. (The General Public License restrictions
16 1.1 mrg do apply in other respects; for example, they cover modification of
17 1.1 mrg the file, and distribution when not linked into a combine
18 1.1 mrg executable.)
19 1.1 mrg
20 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
21 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 1.1 mrg for more details.
24 1.1 mrg
25 1.1 mrg Under Section 7 of GPL version 3, you are granted additional
26 1.1 mrg permissions described in the GCC Runtime Library Exception, version
27 1.1 mrg 3.1, as published by the Free Software Foundation.
28 1.1 mrg
29 1.1 mrg You should have received a copy of the GNU General Public License and
30 1.1 mrg a copy of the GCC Runtime Library Exception along with this program;
31 1.1 mrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
32 1.1 mrg <http://www.gnu.org/licenses/>. */
33 1.1 mrg
34 1.1 mrg
35 1.1 mrg #include "config.h"
36 1.1 mrg #include <ssp/ssp.h>
37 1.1 mrg #ifdef HAVE_STRING_H
38 1.1 mrg # include <string.h>
39 1.1 mrg #endif
40 1.1 mrg
41 1.1 mrg extern void __chk_fail (void) __attribute__((__noreturn__));
42 1.1 mrg
43 1.1 mrg char *
44 1.1 mrg __strcat_chk (char *__restrict__ dest, const char *__restrict__ src,
45 1.1 mrg size_t slen)
46 1.1 mrg {
47 1.1 mrg char *s1 = dest;
48 1.1 mrg const char *s2 = src;
49 1.1 mrg char c;
50 1.1 mrg
51 1.1 mrg do
52 1.1 mrg {
53 1.1 mrg if (slen-- == 0)
54 1.1 mrg __chk_fail ();
55 1.1 mrg c = *s1++;
56 1.1 mrg }
57 1.1 mrg while (c != '\0');
58 1.1 mrg
59 1.1 mrg ++slen;
60 1.1 mrg s1 -= 2;
61 1.1 mrg
62 1.1 mrg do
63 1.1 mrg {
64 1.1 mrg if (slen-- == 0)
65 1.1 mrg __chk_fail ();
66 1.1 mrg c = *s2++;
67 1.1 mrg *++s1 = c;
68 1.1 mrg }
69 1.1 mrg while (c != '\0');
70 1.1 mrg
71 1.1 mrg return dest;
72 1.1 mrg }
73