From 1c50512a2b2262363e327edc19370b2d0339ca11 Mon Sep 17 00:00:00 2001 From: schwarze Date: Sat, 27 Aug 2016 12:08:38 +0000 Subject: [PATCH] improve revision 1.2: in unusual cases, fgetwc(3) can succeed even though ferror(3) is already set; also from Andrey Chernov ; OK millert@ --- lib/libc/stdio/fgetwln.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/libc/stdio/fgetwln.c b/lib/libc/stdio/fgetwln.c index 2ee7e8d61e6..aba11b5b285 100644 --- a/lib/libc/stdio/fgetwln.c +++ b/lib/libc/stdio/fgetwln.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fgetwln.c,v 1.3 2016/08/24 18:35:12 schwarze Exp $ */ +/* $OpenBSD: fgetwln.c,v 1.4 2016/08/27 12:08:38 schwarze Exp $ */ /*- * Copyright (c) 2002-2004 Tim J. Robbins. @@ -69,7 +69,17 @@ fgetwln(FILE * __restrict fp, size_t *lenp) if (wc == L'\n') break; } - if (len == 0 || fp->_flags & __SERR) + + /* + * The following test assumes that fgetwc() fails when + * feof() is already set, and that fgetwc() will never + * set feof() in the same call where it also sets ferror() + * or returns non-WEOF. + * Testing ferror() would not be better because fgetwc() + * may succeed even when ferror() is already set. + */ + + if (len == 0 || (wc == WEOF && !__sfeof(fp))) goto error; FUNLOCKFILE(fp); -- 2.20.1