1*ca2d95d1Schristos /* $NetBSD: gets_chk.c,v 1.7 2013/10/04 20:49:16 christos Exp $ */
22368dc66Stls
32368dc66Stls /*-
42368dc66Stls * Copyright (c) 2006 The NetBSD Foundation, Inc.
52368dc66Stls * All rights reserved.
62368dc66Stls *
72368dc66Stls * This code is derived from software contributed to The NetBSD Foundation
82368dc66Stls * by Christos Zoulas.
92368dc66Stls *
102368dc66Stls * Redistribution and use in source and binary forms, with or without
112368dc66Stls * modification, are permitted provided that the following conditions
122368dc66Stls * are met:
132368dc66Stls * 1. Redistributions of source code must retain the above copyright
142368dc66Stls * notice, this list of conditions and the following disclaimer.
152368dc66Stls * 2. Redistributions in binary form must reproduce the above copyright
162368dc66Stls * notice, this list of conditions and the following disclaimer in the
172368dc66Stls * documentation and/or other materials provided with the distribution.
182368dc66Stls *
192368dc66Stls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202368dc66Stls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212368dc66Stls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222368dc66Stls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232368dc66Stls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242368dc66Stls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252368dc66Stls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262368dc66Stls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272368dc66Stls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282368dc66Stls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292368dc66Stls * POSSIBILITY OF SUCH DAMAGE.
302368dc66Stls */
312368dc66Stls #include <sys/cdefs.h>
32*ca2d95d1Schristos __RCSID("$NetBSD: gets_chk.c,v 1.7 2013/10/04 20:49:16 christos Exp $");
332368dc66Stls
342368dc66Stls /*LINTLIBRARY*/
352368dc66Stls
36dc99372bStls #include <ssp/ssp.h>
372368dc66Stls #include <stdio.h>
382368dc66Stls #include <string.h>
392368dc66Stls #include <limits.h>
402368dc66Stls #include <stdlib.h>
41d0d36b61Skristerw #include <ssp/stdio.h>
422368dc66Stls
43*ca2d95d1Schristos extern char *__gets(char *);
44d9b4f5bbSchristos #undef gets
45d9b4f5bbSchristos
462368dc66Stls char *
__gets_chk(char * __restrict buf,size_t slen)472368dc66Stls __gets_chk(char * __restrict buf, size_t slen)
482368dc66Stls {
492368dc66Stls char *abuf;
502368dc66Stls size_t len;
512368dc66Stls
522368dc66Stls if (slen >= (size_t)INT_MAX)
53*ca2d95d1Schristos return __gets(buf);
542368dc66Stls
552368dc66Stls if ((abuf = malloc(slen + 1)) == NULL)
56*ca2d95d1Schristos return __gets(buf);
572368dc66Stls
58f9884f51Swiz if (fgets(abuf, (int)(slen + 1), stdin) == NULL) {
59f9884f51Swiz free(abuf);
602368dc66Stls return NULL;
61f9884f51Swiz }
622368dc66Stls
632368dc66Stls len = strlen(abuf);
642368dc66Stls if (len > 0 && abuf[len - 1] == '\n')
652368dc66Stls --len;
662368dc66Stls
672368dc66Stls if (len >= slen)
682368dc66Stls __chk_fail();
692368dc66Stls
702368dc66Stls (void)memcpy(buf, abuf, len);
712368dc66Stls
722368dc66Stls buf[len] = '\0';
732368dc66Stls free(abuf);
742368dc66Stls return buf;
752368dc66Stls }
76