1*404b540aSrobert /* Checking gets.
2*404b540aSrobert Copyright (C) 2005 Free Software Foundation, Inc.
3*404b540aSrobert
4*404b540aSrobert This file is part of GCC.
5*404b540aSrobert
6*404b540aSrobert GCC is free software; you can redistribute it and/or modify it under
7*404b540aSrobert the terms of the GNU General Public License as published by the Free
8*404b540aSrobert Software Foundation; either version 2, or (at your option) any later
9*404b540aSrobert version.
10*404b540aSrobert
11*404b540aSrobert In addition to the permissions in the GNU General Public License, the
12*404b540aSrobert Free Software Foundation gives you unlimited permission to link the
13*404b540aSrobert compiled version of this file into combinations with other programs,
14*404b540aSrobert and to distribute those combinations without any restriction coming
15*404b540aSrobert from the use of this file. (The General Public License restrictions
16*404b540aSrobert do apply in other respects; for example, they cover modification of
17*404b540aSrobert the file, and distribution when not linked into a combine
18*404b540aSrobert executable.)
19*404b540aSrobert
20*404b540aSrobert GCC is distributed in the hope that it will be useful, but WITHOUT ANY
21*404b540aSrobert WARRANTY; without even the implied warranty of MERCHANTABILITY or
22*404b540aSrobert FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23*404b540aSrobert for more details.
24*404b540aSrobert
25*404b540aSrobert You should have received a copy of the GNU General Public License
26*404b540aSrobert along with GCC; see the file COPYING. If not, write to the Free
27*404b540aSrobert Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
28*404b540aSrobert 02110-1301, USA. */
29*404b540aSrobert
30*404b540aSrobert /* As a special exception, if you link this library with files compiled with
31*404b540aSrobert GCC to produce an executable, this does not cause the resulting executable
32*404b540aSrobert to be covered by the GNU General Public License. This exception does not
33*404b540aSrobert however invalidate any other reasons why the executable file might be
34*404b540aSrobert covered by the GNU General Public License. */
35*404b540aSrobert
36*404b540aSrobert #include "config.h"
37*404b540aSrobert #include <ssp/ssp.h>
38*404b540aSrobert #include <stdarg.h>
39*404b540aSrobert #ifdef HAVE_STDLIB_H
40*404b540aSrobert # include <stdlib.h>
41*404b540aSrobert #endif
42*404b540aSrobert #ifdef HAVE_ALLOCA_H
43*404b540aSrobert # include <alloca.h>
44*404b540aSrobert #endif
45*404b540aSrobert #ifdef HAVE_LIMITS_H
46*404b540aSrobert # include <limits.h>
47*404b540aSrobert #endif
48*404b540aSrobert #ifdef HAVE_STDIO_H
49*404b540aSrobert # include <stdio.h>
50*404b540aSrobert #endif
51*404b540aSrobert #ifdef HAVE_STRING_H
52*404b540aSrobert # include <string.h>
53*404b540aSrobert #endif
54*404b540aSrobert
55*404b540aSrobert extern void __chk_fail (void) __attribute__((__noreturn__));
56*404b540aSrobert
57*404b540aSrobert char *
__gets_chk(char * s,size_t slen)58*404b540aSrobert __gets_chk (char *s, size_t slen)
59*404b540aSrobert {
60*404b540aSrobert char *ret, *buf;
61*404b540aSrobert
62*404b540aSrobert if (slen >= (size_t) INT_MAX)
63*404b540aSrobert return gets (s);
64*404b540aSrobert
65*404b540aSrobert if (slen <= 8192)
66*404b540aSrobert buf = alloca (slen + 1);
67*404b540aSrobert else
68*404b540aSrobert buf = malloc (slen + 1);
69*404b540aSrobert if (buf == NULL)
70*404b540aSrobert return gets (s);
71*404b540aSrobert
72*404b540aSrobert ret = fgets (buf, (int) (slen + 1), stdin);
73*404b540aSrobert if (ret != NULL)
74*404b540aSrobert {
75*404b540aSrobert size_t len = strlen (buf);
76*404b540aSrobert if (len > 0 && buf[len - 1] == '\n')
77*404b540aSrobert --len;
78*404b540aSrobert if (len == slen)
79*404b540aSrobert __chk_fail ();
80*404b540aSrobert memcpy (s, buf, len);
81*404b540aSrobert s[len] = '\0';
82*404b540aSrobert ret = s;
83*404b540aSrobert }
84*404b540aSrobert
85*404b540aSrobert if (slen > 8192)
86*404b540aSrobert free (buf);
87*404b540aSrobert return ret;
88*404b540aSrobert }
89