svn commit: r305413 - head/lib/libc/stdio

Andrey A. Chernov ache at FreeBSD.org
Mon Sep 5 06:46:05 UTC 2016


Author: ache
Date: Mon Sep  5 06:46:04 2016
New Revision: 305413
URL: https://svnweb.freebsd.org/changeset/base/305413

Log:
  Fix error handling.
  
  MFC after:      3 days

Modified:
  head/lib/libc/stdio/fgets.c

Modified: head/lib/libc/stdio/fgets.c
==============================================================================
--- head/lib/libc/stdio/fgets.c	Mon Sep  5 06:10:51 2016	(r305412)
+++ head/lib/libc/stdio/fgets.c	Mon Sep  5 06:46:04 2016	(r305413)
@@ -37,6 +37,7 @@ static char sccsid[] = "@(#)fgets.c	8.2 
 __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
 	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
 		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);
 				}


More information about the svn-src-all mailing list