Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/stdio/gets_s.c
/*- | /*- | ||||
* SPDX-License-Identifier: BSD-3-Clause | * SPDX-License-Identifier: BSD-3-Clause | ||||
* | * | ||||
* Copyright (c) 1990, 1993 | * Copyright (c) 1990, 1993 | ||||
* The Regents of the University of California. All rights reserved. | * The Regents of the University of California. All rights reserved. | ||||
* Copyright (c) 2017, 2018 | |||||
* Cyril S. E. Schubert. All rights reserved. | |||||
* | * | ||||
* This code is derived from software contributed to Berkeley by | * This code is derived from software contributed to Berkeley by | ||||
* Chris Torek. | * Chris Torek. | ||||
* | * | ||||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
* modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||
* are met: | * are met: | ||||
* 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | ||||
* notice, this list of conditions and the following disclaimer. | * notice, this list of conditions and the following disclaimer. | ||||
* 2. Redistributions in binary form must reproduce the above copyright | * 2. Redistributions in binary form must reproduce the above copyright | ||||
ed: Do you want this code to be 3-clause BSD licensed on your behalf as well? If so, leave this as… | |||||
Not Done Inline ActionsI think I'll do that. I won't mark this done until it is. cy: I think I'll do that. I won't mark this done until it is. | |||||
* notice, this list of conditions and the following disclaimer in the | * notice, this list of conditions and the following disclaimer in the | ||||
* documentation and/or other materials provided with the distribution. | * documentation and/or other materials provided with the distribution. | ||||
* 3. Neither the name of the University nor the names of its contributors | * 3. Neither the name of the University nor the names of its contributors | ||||
* may be used to endorse or promote products derived from this software | * may be used to endorse or promote products derived from this software | ||||
* without specific prior written permission. | * without specific prior written permission. | ||||
* | * | ||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||||
* SUCH DAMAGE. | * SUCH DAMAGE. | ||||
*/ | */ | ||||
#if defined(LIBC_SCCS) && !defined(lint) | |||||
static char sccsid[] = "@(#)gets.c 8.1 (Berkeley) 6/4/93"; | |||||
#endif /* LIBC_SCCS and not lint */ | |||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include "namespace.h" | #include "namespace.h" | ||||
#include <errno.h> | |||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <stdint.h> | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include "un-namespace.h" | #include "un-namespace.h" | ||||
#include "libc_private.h" | #include "libc_private.h" | ||||
#include "local.h" | #include "local.h" | ||||
__warn_references(gets, "warning: this program uses gets(), which is unsafe."); | static inline char * | ||||
_gets_s(char *buf, rsize_t n) | |||||
char * | |||||
gets(char *buf) | |||||
{ | { | ||||
int c; | int c; | ||||
char *s, *ret; | char *s; | ||||
static int warned; | |||||
static const char w[] = | |||||
"warning: this program uses gets(), which is unsafe.\n"; | |||||
FLOCKFILE_CANCELSAFE(stdin); | |||||
ORIENT(stdin, -1); | ORIENT(stdin, -1); | ||||
if (!warned) { | for (s = buf, n--; (c = __sgetc(stdin)) != '\n' && n > 0 ; n--) { | ||||
Not Done Inline ActionsStyle nit: space after return (several times) jhb: Style nit: space after return (several times) | |||||
(void) _write(STDERR_FILENO, w, sizeof(w) - 1); | |||||
warned = 1; | |||||
} | |||||
for (s = buf; (c = __sgetc(stdin)) != '\n'; ) { | |||||
if (c == EOF) { | if (c == EOF) { | ||||
if (s == buf) { | if (s == buf) { | ||||
ret = NULL; | return (NULL); | ||||
goto end; | |||||
} else | } else | ||||
break; | break; | ||||
} else | } else | ||||
*s++ = c; | *s++ = c; | ||||
} | } | ||||
/* | |||||
* If end of buffer reached, discard until \n or eof. | |||||
* Then throw an error. | |||||
*/ | |||||
Done Inline ActionsI'd suggest putting { }'s around this block to prevent potential 'trailing else' issues from being introduced when refactoring. ed: I'd suggest putting { }'s around this block to prevent potential 'trailing else' issues from… | |||||
Not Done Inline ActionsFixed. cy: Fixed. | |||||
if (n == 0) { | |||||
/* discard */ | |||||
while ((c = __sgetc(stdin)) != '\n' && c != EOF); | |||||
/* throw the error after lock released prior to exit */ | |||||
__throw_constraint_handler_s("gets_s : end of buffer", E2BIG); | |||||
return (NULL); | |||||
} | |||||
*s = 0; | *s = 0; | ||||
ret = buf; | return (buf); | ||||
end: | } | ||||
Done Inline ActionsThere is no need to have these empty lined in the comment block, right? ed: There is no need to have these empty lined in the comment block, right? | |||||
/* ISO/IEC 9899:2011 K.3.7.4.1 */ | |||||
char * | |||||
gets_s(char *buf, rsize_t n) | |||||
{ | |||||
char *ret; | |||||
Done Inline ActionsUnnecessary space. ed: Unnecessary space. | |||||
if (buf == NULL) { | |||||
__throw_constraint_handler_s("gets_s : str is NULL", EINVAL); | |||||
Done Inline ActionsYou can remove one pair of parentheses here. ed: You can remove one pair of parentheses here. | |||||
return(NULL); | |||||
Done Inline ActionsI actually think this is simpler if you just avoid the goto and duplicate the one line to unlock: FUNLOCKFILE_CANCELSAFE(); __throw_constraint_handler_s("gets_s : end of buffer", E2BIG); return (NULL); You can then remove the 'throw_error' variable and the 'end:' label. jhb: I actually think this is simpler if you just avoid the goto and duplicate the one line to… | |||||
} else if (n > RSIZE_MAX) { | |||||
__throw_constraint_handler_s("gets_s : n > RSIZE_MAX", | |||||
EINVAL); | |||||
return(NULL); | |||||
} else if (n == 0) { | |||||
__throw_constraint_handler_s("gets_s : n == 0", EINVAL); | |||||
return(NULL); | |||||
} | |||||
FLOCKFILE_CANCELSAFE(stdin); | |||||
ret = _gets_s(buf, n); | |||||
FUNLOCKFILE_CANCELSAFE(); | FUNLOCKFILE_CANCELSAFE(); | ||||
return (ret); | return (ret); | ||||
} | } |
Do you want this code to be 3-clause BSD licensed on your behalf as well? If so, leave this as is. Otherwise, remove your name from the copyright above and add a second (2-clause BSD?) license to this source file.