xref: /dflybsd-src/contrib/gcc-4.7/libssp/gets-chk.c (revision 94b98a915ba84699b2a2adab9146fbc2cf617459)
1*f02514dfSJohn Marino /* Checking gets.
2*f02514dfSJohn Marino    Copyright (C) 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 #include <stdarg.h>
38*f02514dfSJohn Marino #ifdef HAVE_STDLIB_H
39*f02514dfSJohn Marino # include <stdlib.h>
40*f02514dfSJohn Marino #endif
41*f02514dfSJohn Marino #ifdef HAVE_ALLOCA_H
42*f02514dfSJohn Marino # include <alloca.h>
43*f02514dfSJohn Marino #endif
44*f02514dfSJohn Marino #ifdef HAVE_LIMITS_H
45*f02514dfSJohn Marino # include <limits.h>
46*f02514dfSJohn Marino #endif
47*f02514dfSJohn Marino #ifdef HAVE_STDIO_H
48*f02514dfSJohn Marino # include <stdio.h>
49*f02514dfSJohn Marino #endif
50*f02514dfSJohn Marino #ifdef HAVE_STRING_H
51*f02514dfSJohn Marino # include <string.h>
52*f02514dfSJohn Marino #endif
53*f02514dfSJohn Marino 
54*f02514dfSJohn Marino extern void __chk_fail (void) __attribute__((__noreturn__));
55*f02514dfSJohn Marino 
56*f02514dfSJohn Marino char *
__gets_chk(char * s,size_t slen)57*f02514dfSJohn Marino __gets_chk (char *s, size_t slen)
58*f02514dfSJohn Marino {
59*f02514dfSJohn Marino   char *ret, *buf;
60*f02514dfSJohn Marino 
61*f02514dfSJohn Marino   if (slen >= (size_t) INT_MAX)
62*f02514dfSJohn Marino     return gets (s);
63*f02514dfSJohn Marino 
64*f02514dfSJohn Marino   if (slen <= 8192)
65*f02514dfSJohn Marino     buf = alloca (slen + 1);
66*f02514dfSJohn Marino   else
67*f02514dfSJohn Marino     buf = malloc (slen + 1);
68*f02514dfSJohn Marino   if (buf == NULL)
69*f02514dfSJohn Marino     return gets (s);
70*f02514dfSJohn Marino 
71*f02514dfSJohn Marino   ret = fgets (buf, (int) (slen + 1), stdin);
72*f02514dfSJohn Marino   if (ret != NULL)
73*f02514dfSJohn Marino     {
74*f02514dfSJohn Marino       size_t len = strlen (buf);
75*f02514dfSJohn Marino       if (len > 0 && buf[len - 1] == '\n')
76*f02514dfSJohn Marino         --len;
77*f02514dfSJohn Marino       if (len == slen)
78*f02514dfSJohn Marino         __chk_fail ();
79*f02514dfSJohn Marino       memcpy (s, buf, len);
80*f02514dfSJohn Marino       s[len] = '\0';
81*f02514dfSJohn Marino       ret = s;
82*f02514dfSJohn Marino     }
83*f02514dfSJohn Marino 
84*f02514dfSJohn Marino   if (slen > 8192)
85*f02514dfSJohn Marino     free (buf);
86*f02514dfSJohn Marino   return ret;
87*f02514dfSJohn Marino }
88