Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/stdio Improvements to fgetwln(3) from Andrey Chernov:



details:   https://anonhg.NetBSD.org/src/rev/7fcc85f97ea6
branches:  trunk
changeset: 817621:7fcc85f97ea6
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Aug 27 13:15:48 2016 +0000

description:
Improvements to fgetwln(3) from Andrey Chernov:

1) Set the stdio error indicator on __slbexpand() failure.

2) fgetwc(3) may succeed even when ferror(3) is already set
   (for example, consider a program using SIG_IGN on SIGTTIN,
   reading from the tty while in the background, getting EIO,
   then coming to the foreground and reading again).
   So do not force fgetwln(3) to fail in such a case either.
   (Yes, the program should probably clearerr(3) before
   reading again, but let's be nicer in case it forgets.)

diffstat:

 lib/libc/stdio/fgetwln.c |  20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)

diffs (46 lines):

diff -r 8f0dc526dd8f -r 7fcc85f97ea6 lib/libc/stdio/fgetwln.c
--- a/lib/libc/stdio/fgetwln.c  Sat Aug 27 12:08:14 2016 +0000
+++ b/lib/libc/stdio/fgetwln.c  Sat Aug 27 13:15:48 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fgetwln.c,v 1.7 2016/08/22 07:41:10 christos Exp $     */
+/*     $NetBSD: fgetwln.c,v 1.8 2016/08/27 13:15:48 christos Exp $     */
 
 /*-
  * Copyright (c) 2002-2004 Tim J. Robbins.
@@ -31,7 +31,7 @@
 #if 0
 __FBSDID("$FreeBSD: src/lib/libc/stdio/fgetwln.c,v 1.2 2004/08/06 17:00:09 tjr Exp $");
 #else
-__RCSID("$NetBSD: fgetwln.c,v 1.7 2016/08/22 07:41:10 christos Exp $");
+__RCSID("$NetBSD: fgetwln.c,v 1.8 2016/08/27 13:15:48 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -88,13 +88,25 @@
        while ((wc = __fgetwc_unlock(fp)) != WEOF) {
 #define        GROW    512
                if (len * sizeof(wchar_t) >= _EXT(fp)->_fgetstr_len &&
-                   __slbexpand(fp, (len + GROW) * sizeof(wchar_t)))
+                   __slbexpand(fp, (len + GROW) * sizeof(wchar_t))) {
+                       fp->_flags |= __SERR;
                        goto error;
+               }
                *((wchar_t *)(void *)_EXT(fp)->_fgetstr_buf + len++) = wc;
                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);



Home | Main Index | Thread Index | Old Index