From 47025c121aa7dac65253649685f905043217872f Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 5 Sep 2016 01:57:32 +0000 Subject: [PATCH] MFC r305241 fgetwc(3) may set both __SEOF and __SERR at once (in case of incomplete sequence near EOF), so we can't just check for (wc == WEOF && !__sfeof(fp)) and must relay on __sferror(fp) with __SERR clearing/restoring. git-svn-id: svn://svn.freebsd.org/base/stable/10@305403 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- lib/libc/stdio/fgetwln.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/libc/stdio/fgetwln.c b/lib/libc/stdio/fgetwln.c index 34a80a076..037657c22 100644 --- a/lib/libc/stdio/fgetwln.c +++ b/lib/libc/stdio/fgetwln.c @@ -47,11 +47,16 @@ fgetwln_l(FILE * __restrict fp, size_t *lenp, locale_t locale) { wint_t wc; size_t len; + int savserr; + FIX_LOCALE(locale); FLOCKFILE(fp); ORIENT(fp, 1); + savserr = fp->_flags & __SERR; + fp->_flags &= ~__SERR; + len = 0; while ((wc = __fgetwc(fp, locale)) != WEOF) { #define GROW 512 @@ -64,7 +69,12 @@ fgetwln_l(FILE * __restrict fp, size_t *lenp, locale_t locale) if (wc == L'\n') break; } - if (len == 0 || (wc == WEOF && !__sfeof(fp))) + /* fgetwc(3) may set both __SEOF and __SERR at once. */ + if (__sferror(fp)) + goto error; + + fp->_flags |= savserr; + if (len == 0) goto error; FUNLOCKFILE(fp); -- 2.45.0