summaryrefslogtreecommitdiffstats
path: root/lib/libc
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>2016-09-08 07:14:48 +0000
committerache <ache@FreeBSD.org>2016-09-08 07:14:48 +0000
commit11eb45c08752b180e8521ee076638d3e021bc9db (patch)
treea7b689fdd8cd6809acae1e5ffe01ad39f86ee1a8 /lib/libc
parent56ade7a489e1dc176faf0206cbed2f8659a92648 (diff)
downloadFreeBSD-src-11eb45c08752b180e8521ee076638d3e021bc9db.zip
FreeBSD-src-11eb45c08752b180e8521ee076638d3e021bc9db.tar.gz
MFC r305413
Fix error handling.
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/stdio/fgets.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/lib/libc/stdio/fgets.c b/lib/libc/stdio/fgets.c
index 9abf559..a2e39ed 100644
--- a/lib/libc/stdio/fgets.c
+++ b/lib/libc/stdio/fgets.c
@@ -37,6 +37,7 @@ static char sccsid[] = "@(#)fgets.c 8.2 (Berkeley) 12/22/93";
__FBSDID("$FreeBSD$");
#include "namespace.h"
+#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "un-namespace.h"
@@ -55,11 +56,16 @@ fgets(char * __restrict buf, int n, FILE * __restrict fp)
char *s;
unsigned char *p, *t;
- if (n <= 0) /* sanity check */
- return (NULL);
-
FLOCKFILE(fp);
ORIENT(fp, -1);
+
+ if (n <= 0) { /* sanity check */
+ fp->_flags |= __SERR;
+ errno = EINVAL;
+ FUNLOCKFILE(fp);
+ return (NULL);
+ }
+
s = buf;
n--; /* leave space for NUL */
while (n != 0) {
@@ -69,7 +75,7 @@ fgets(char * __restrict buf, int n, FILE * __restrict fp)
if ((len = fp->_r) <= 0) {
if (__srefill(fp)) {
/* EOF/error: stop with partial or no line */
- if (s == buf) {
+ if (!__sfeof(fp) || s == buf) {
FUNLOCKFILE(fp);
return (NULL);
}
OpenPOWER on IntegriCloud