diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2016-08-25 19:21:34 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2016-08-25 19:21:34 +0000 |
commit | 494d2e32f9e83938f4b9f94f281adbcaecee43aa (patch) | |
tree | acb02eae8fbfa4752f89c3328c8dfac4f448f24d /lib | |
parent | e78ca96e1113c4ee127ef6622c8cd7f91c023525 (diff) |
After read errors, fgetln(3) sometimes succeeded (returning non-NULL)
and failed (setting errno and ferror(3)) both at the same time.
That's a bad idea in general, and here in particular since
returning partial lines was neither reliable (sometimes, you
got NULL anyway) nor predictable (almost always, the line would
be truncated long before the actual read error).
Instead, on read failure, fail properly and always return NULL.
Issue found in a discussion with Andrey Chernov <ache at freebsd dot org>
who finally agreed to move FreeBSD into the same direction.
The fix is joint work with and OK by millert@.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/stdio/fgetln.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/lib/libc/stdio/fgetln.c b/lib/libc/stdio/fgetln.c index ae2eb10f96c..a5ea1b3207e 100644 --- a/lib/libc/stdio/fgetln.c +++ b/lib/libc/stdio/fgetln.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fgetln.c,v 1.14 2015/08/31 02:53:57 guenther Exp $ */ +/* $OpenBSD: fgetln.c,v 1.15 2016/08/25 19:21:33 schwarze Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -115,8 +115,11 @@ fgetln(FILE *fp, size_t *lenp) (void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p, len - off); off = len; - if (__srefill(fp)) - break; /* EOF or error: return partial line */ + if (__srefill(fp)) { + if (fp->_flags & __SEOF) + break; + goto error; + } if ((p = memchr((void *)fp->_p, '\n', fp->_r)) == NULL) continue; |