1169695Skan/* Checking gets. 2169695Skan Copyright (C) 2005 Free Software Foundation, Inc. 3169695Skan 4169695SkanThis file is part of GCC. 5169695Skan 6169695SkanGCC is free software; you can redistribute it and/or modify it under 7169695Skanthe terms of the GNU General Public License as published by the Free 8169695SkanSoftware Foundation; either version 2, or (at your option) any later 9169695Skanversion. 10169695Skan 11169695SkanIn addition to the permissions in the GNU General Public License, the 12169695SkanFree Software Foundation gives you unlimited permission to link the 13169695Skancompiled version of this file into combinations with other programs, 14169695Skanand to distribute those combinations without any restriction coming 15169695Skanfrom the use of this file. (The General Public License restrictions 16169695Skando apply in other respects; for example, they cover modification of 17169695Skanthe file, and distribution when not linked into a combine 18169695Skanexecutable.) 19169695Skan 20169695SkanGCC is distributed in the hope that it will be useful, but WITHOUT ANY 21169695SkanWARRANTY; without even the implied warranty of MERCHANTABILITY or 22169695SkanFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 23169695Skanfor more details. 24169695Skan 25169695SkanYou should have received a copy of the GNU General Public License 26169695Skanalong with GCC; see the file COPYING. If not, write to the Free 27169695SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 28169695Skan02110-1301, USA. */ 29169695Skan 30169695Skan/* As a special exception, if you link this library with files compiled with 31169695Skan GCC to produce an executable, this does not cause the resulting executable 32169695Skan to be covered by the GNU General Public License. This exception does not 33169695Skan however invalidate any other reasons why the executable file might be 34169695Skan covered by the GNU General Public License. */ 35169695Skan 36169695Skan#include "config.h" 37169695Skan#include <ssp/ssp.h> 38169695Skan#include <stdarg.h> 39169695Skan#ifdef HAVE_STDLIB_H 40169695Skan# include <stdlib.h> 41169695Skan#endif 42169695Skan#ifdef HAVE_ALLOCA_H 43169695Skan# include <alloca.h> 44169695Skan#endif 45169695Skan#ifdef HAVE_LIMITS_H 46169695Skan# include <limits.h> 47169695Skan#endif 48169695Skan#ifdef HAVE_STDIO_H 49169695Skan# include <stdio.h> 50169695Skan#endif 51169695Skan#ifdef HAVE_STRING_H 52169695Skan# include <string.h> 53169695Skan#endif 54169695Skan 55169695Skanextern void __chk_fail (void) __attribute__((__noreturn__)); 56169695Skan 57169695Skanchar * 58169695Skan__gets_chk (char *s, size_t slen) 59169695Skan{ 60169695Skan char *ret, *buf; 61169695Skan 62169695Skan if (slen >= (size_t) INT_MAX) 63169695Skan return gets (s); 64169695Skan 65169695Skan if (slen <= 8192) 66169695Skan buf = alloca (slen + 1); 67169695Skan else 68169695Skan buf = malloc (slen + 1); 69169695Skan if (buf == NULL) 70169695Skan return gets (s); 71169695Skan 72169695Skan ret = fgets (buf, (int) (slen + 1), stdin); 73169695Skan if (ret != NULL) 74169695Skan { 75169695Skan size_t len = strlen (buf); 76169695Skan if (len > 0 && buf[len - 1] == '\n') 77169695Skan --len; 78169695Skan if (len == slen) 79169695Skan __chk_fail (); 80169695Skan memcpy (s, buf, len); 81169695Skan s[len] = '\0'; 82169695Skan ret = s; 83169695Skan } 84169695Skan 85169695Skan if (slen > 8192) 86169695Skan free (buf); 87169695Skan return ret; 88169695Skan} 89