xref: /netbsd-src/external/gpl3/gcc.old/dist/libssp/gets-chk.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg /* Checking gets.
2*8feb0f0bSmrg    Copyright (C) 2005-2020 Free Software Foundation, Inc.
336ac495dSmrg 
436ac495dSmrg This file is part of GCC.
536ac495dSmrg 
636ac495dSmrg GCC is free software; you can redistribute it and/or modify it under
736ac495dSmrg the terms of the GNU General Public License as published by the Free
836ac495dSmrg Software Foundation; either version 3, or (at your option) any later
936ac495dSmrg version.
1036ac495dSmrg 
1136ac495dSmrg In addition to the permissions in the GNU General Public License, the
1236ac495dSmrg Free Software Foundation gives you unlimited permission to link the
1336ac495dSmrg compiled version of this file into combinations with other programs,
1436ac495dSmrg and to distribute those combinations without any restriction coming
1536ac495dSmrg from the use of this file.  (The General Public License restrictions
1636ac495dSmrg do apply in other respects; for example, they cover modification of
1736ac495dSmrg the file, and distribution when not linked into a combine
1836ac495dSmrg executable.)
1936ac495dSmrg 
2036ac495dSmrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
2136ac495dSmrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
2236ac495dSmrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2336ac495dSmrg for more details.
2436ac495dSmrg 
2536ac495dSmrg Under Section 7 of GPL version 3, you are granted additional
2636ac495dSmrg permissions described in the GCC Runtime Library Exception, version
2736ac495dSmrg 3.1, as published by the Free Software Foundation.
2836ac495dSmrg 
2936ac495dSmrg You should have received a copy of the GNU General Public License and
3036ac495dSmrg a copy of the GCC Runtime Library Exception along with this program;
3136ac495dSmrg see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
3236ac495dSmrg <http://www.gnu.org/licenses/>.  */
3336ac495dSmrg 
3436ac495dSmrg 
3536ac495dSmrg #include "config.h"
3636ac495dSmrg #include <ssp/ssp.h>
3736ac495dSmrg #include <stdarg.h>
3836ac495dSmrg #ifdef HAVE_STDLIB_H
3936ac495dSmrg # include <stdlib.h>
4036ac495dSmrg #endif
4136ac495dSmrg #ifdef HAVE_ALLOCA_H
4236ac495dSmrg # include <alloca.h>
4336ac495dSmrg #endif
4436ac495dSmrg #ifdef HAVE_LIMITS_H
4536ac495dSmrg # include <limits.h>
4636ac495dSmrg #endif
4736ac495dSmrg #ifdef HAVE_STDIO_H
4836ac495dSmrg # include <stdio.h>
4936ac495dSmrg #endif
5036ac495dSmrg #ifdef HAVE_STRING_H
5136ac495dSmrg # include <string.h>
5236ac495dSmrg #endif
5336ac495dSmrg 
5436ac495dSmrg #if !(!defined __USE_ISOC11				\
5536ac495dSmrg       || (defined __cplusplus && __cplusplus <= 201103L))
5636ac495dSmrg extern char *gets (char *);
5736ac495dSmrg #endif
5836ac495dSmrg 
5936ac495dSmrg extern void __chk_fail (void) __attribute__((__noreturn__));
6036ac495dSmrg 
6136ac495dSmrg char *
__gets_chk(char * s,size_t slen)6236ac495dSmrg __gets_chk (char *s, size_t slen)
6336ac495dSmrg {
6436ac495dSmrg   char *ret, *buf;
6536ac495dSmrg 
6636ac495dSmrg   if (slen >= (size_t) INT_MAX)
6736ac495dSmrg     return gets (s);
6836ac495dSmrg 
6936ac495dSmrg   if (slen <= 8192)
7036ac495dSmrg     buf = alloca (slen + 1);
7136ac495dSmrg   else
7236ac495dSmrg     buf = malloc (slen + 1);
7336ac495dSmrg   if (buf == NULL)
7436ac495dSmrg     return gets (s);
7536ac495dSmrg 
7636ac495dSmrg   ret = fgets (buf, (int) (slen + 1), stdin);
7736ac495dSmrg   if (ret != NULL)
7836ac495dSmrg     {
7936ac495dSmrg       size_t len = strlen (buf);
8036ac495dSmrg       if (len > 0 && buf[len - 1] == '\n')
8136ac495dSmrg         --len;
8236ac495dSmrg       if (len == slen)
8336ac495dSmrg         __chk_fail ();
8436ac495dSmrg       memcpy (s, buf, len);
8536ac495dSmrg       s[len] = '\0';
8636ac495dSmrg       ret = s;
8736ac495dSmrg     }
8836ac495dSmrg 
8936ac495dSmrg   if (slen > 8192)
9036ac495dSmrg     free (buf);
9136ac495dSmrg   return ret;
9236ac495dSmrg }
93