Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Sep 2016 07:14:48 +0000 (UTC)
From:      "Andrey A. Chernov" <ache@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r305583 - stable/10/lib/libc/stdio
Message-ID:  <201609080714.u887Ems0007917@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ache
Date: Thu Sep  8 07:14:48 2016
New Revision: 305583
URL: https://svnweb.freebsd.org/changeset/base/305583

Log:
  MFC r305413
  
  Fix error handling.

Modified:
  stable/10/lib/libc/stdio/fgets.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/lib/libc/stdio/fgets.c
==============================================================================
--- stable/10/lib/libc/stdio/fgets.c	Thu Sep  8 06:53:18 2016	(r305582)
+++ stable/10/lib/libc/stdio/fgets.c	Thu Sep  8 07:14:48 2016	(r305583)
@@ -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);
 				}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201609080714.u887Ems0007917>