123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- #include "config.h"
- #include <ssp/ssp.h>
- #include <stdarg.h>
- #ifdef HAVE_STDLIB_H
- # include <stdlib.h>
- #endif
- #ifdef HAVE_ALLOCA_H
- # include <alloca.h>
- #endif
- #ifdef HAVE_LIMITS_H
- # include <limits.h>
- #endif
- #ifdef HAVE_STDIO_H
- # include <stdio.h>
- #endif
- #ifdef HAVE_STRING_H
- # include <string.h>
- #endif
- #if !(!defined __USE_ISOC11 \
- || (defined __cplusplus && __cplusplus <= 201103L))
- extern char *gets (char *);
- #endif
- extern void __chk_fail (void) __attribute__((__noreturn__));
- char *
- __gets_chk (char *s, size_t slen)
- {
- char *ret, *buf;
- if (slen >= (size_t) INT_MAX)
- return gets (s);
- if (slen <= 8192)
- buf = alloca (slen + 1);
- else
- buf = malloc (slen + 1);
- if (buf == NULL)
- return gets (s);
- ret = fgets (buf, (int) (slen + 1), stdin);
- if (ret != NULL)
- {
- size_t len = strlen (buf);
- if (len > 0 && buf[len - 1] == '\n')
- --len;
- if (len == slen)
- __chk_fail ();
- memcpy (s, buf, len);
- s[len] = '\0';
- ret = s;
- }
- if (slen > 8192)
- free (buf);
- return ret;
- }
|