From: guenther Date: Fri, 30 Aug 2024 03:44:48 +0000 (+0000) Subject: Revert commitid KcCtsA53F9UQzc0t: X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=0f4fed2b777706cdd6a28922ee70a2f2b12f2b46;p=openbsd Revert commitid KcCtsA53F9UQzc0t: "Make exit(), fclose(), fflush(), and freopen() comply with POSIX-2008 requirements for setting the underlying file position when flushing read-mode streams, and make an fseek()-after-fflush() not change the underlying file position." Something isn't correct about it and it breaks at least initdb from the postgresql-server package. discussed with tb@, semarie@, and deraadt@ --- diff --git a/lib/libc/stdio/fclose.3 b/lib/libc/stdio/fclose.3 index cedcaed15ed..91b22448d22 100644 --- a/lib/libc/stdio/fclose.3 +++ b/lib/libc/stdio/fclose.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: fclose.3,v 1.10 2024/08/12 20:53:09 guenther Exp $ +.\" $OpenBSD: fclose.3,v 1.11 2024/08/30 03:44:48 guenther Exp $ .\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -31,7 +31,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd $Mdocdate: August 12 2024 $ +.Dd $Mdocdate: August 30 2024 $ .Dt FCLOSE 3 .Os .Sh NAME @@ -47,11 +47,8 @@ The function dissociates the named .Fa stream from its underlying file or set of functions. -If the stream was being used for output then any buffered data is written -first, -while if the stream was being used for input then the underlying -file position may be updated, -as if via +If the stream was being used for output, any buffered data is written +first, using .Xr fflush 3 . .Sh RETURN VALUES Upon successful completion 0 is returned. @@ -86,7 +83,7 @@ or The .Fn fclose function conforms to -.St -p1003.1-2024 . +.St -ansiC . .Sh HISTORY The .Fn fclose diff --git a/lib/libc/stdio/fclose.c b/lib/libc/stdio/fclose.c index 07026faccd4..6324a1164c6 100644 --- a/lib/libc/stdio/fclose.c +++ b/lib/libc/stdio/fclose.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fclose.c,v 1.11 2024/08/12 20:53:09 guenther Exp $ */ +/* $OpenBSD: fclose.c,v 1.12 2024/08/30 03:44:48 guenther Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -47,7 +47,7 @@ fclose(FILE *fp) } FLOCKFILE(fp); WCIO_FREE(fp); - r = __sflush(fp); + r = fp->_flags & __SWR ? __sflush(fp) : 0; if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) r = EOF; if (fp->_flags & __SMBF) diff --git a/lib/libc/stdio/fflush.3 b/lib/libc/stdio/fflush.3 index 0ce4579cf44..7b9112acd80 100644 --- a/lib/libc/stdio/fflush.3 +++ b/lib/libc/stdio/fflush.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: fflush.3,v 1.16 2024/08/13 05:52:09 jmc Exp $ +.\" $OpenBSD: fflush.3,v 1.17 2024/08/30 03:44:48 guenther Exp $ .\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -31,7 +31,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd $Mdocdate: August 13 2024 $ +.Dd $Mdocdate: August 30 2024 $ .Dt FFLUSH 3 .Os .Sh NAME @@ -84,9 +84,7 @@ For input streams this discards any input read from the underlying object but not yet obtained via .Xr getc 3 ; this includes any text pushed back via -.Xr ungetc 3 -or -.Xr ungetwc 3 . +.Xr ungetc 3 . .Sh RETURN VALUES Upon successful completion 0 is returned. Otherwise, @@ -98,7 +96,9 @@ is set to indicate the error. .Bl -tag -width Er .It Bq Er EBADF .Fa stream -is not an open stream. +is not an open stream or, in the case of +.Fn fflush , +not a stream open for writing. .El .Pp The function @@ -116,7 +116,7 @@ for any of the errors specified for the routine The .Fn fflush function conforms to -.St -p1003.1-2024 . +.St -ansiC . .Sh HISTORY A predecessor .Fn flush diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c index 5888e2364c8..15b9523c508 100644 --- a/lib/libc/stdio/fflush.c +++ b/lib/libc/stdio/fflush.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fflush.c,v 1.10 2024/08/12 20:53:09 guenther Exp $ */ +/* $OpenBSD: fflush.c,v 1.11 2024/08/30 03:44:48 guenther Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -33,7 +33,6 @@ #include #include -#include #include "local.h" /* Flush a single file, or (if fp is NULL) all files. */ @@ -45,7 +44,11 @@ fflush(FILE *fp) if (fp == NULL) return (_fwalk(__sflush_locked)); FLOCKFILE(fp); - r = __sflush(fp); + if ((fp->_flags & (__SWR | __SRW)) == 0) { + errno = EBADF; + r = EOF; + } else + r = __sflush(fp); FUNLOCKFILE(fp); return (r); } @@ -55,51 +58,30 @@ int __sflush(FILE *fp) { unsigned char *p; - fpos_t off; int n, t; t = fp->_flags; - if (t & __SWR) { - if ((p = fp->_bf._base) == NULL) - return (0); + if ((t & __SWR) == 0) + return (0); - n = fp->_p - p; /* write this much */ + if ((p = fp->_bf._base) == NULL) + return (0); - /* - * Set these immediately to avoid problems with longjmp and to - * allow exchange buffering (via setvbuf) in user write - * function. - */ - fp->_p = p; - fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size; + n = fp->_p - p; /* write this much */ - for (; n > 0; n -= t, p += t) { - t = (*fp->_write)(fp->_cookie, (char *)p, n); - if (t <= 0) { - fp->_flags |= __SERR; - return (EOF); - } - } - } else if ((t & __SRD) && !(t & __SEOF)) { - if (fp->_seek != __sseek || fp->_file < 0) { - errno = EBADF; - return EOF; - } + /* + * Set these immediately to avoid problems with longjmp and to allow + * exchange buffering (via setvbuf) in user write function. + */ + fp->_p = p; + fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size; - off = fp->_r; - if (HASUB(fp)) { - off += fp->_ur; - FREEUB(fp); + for (; n > 0; n -= t, p += t) { + t = (*fp->_write)(fp->_cookie, (char *)p, n); + if (t <= 0) { + fp->_flags |= __SERR; + return (EOF); } - if (t & __SOFF) { - off = fp->_offset - off; - __sseek(fp->_cookie, off, SEEK_SET); - } else if (off != 0) - __sseek(fp->_cookie, -off, SEEK_CUR); - - WCIO_FREE(fp); - fp->_p = fp->_bf._base; - fp->_r = 0; } return (0); } diff --git a/lib/libc/stdio/freopen.c b/lib/libc/stdio/freopen.c index 2b82300c0b2..94cde8edd68 100644 --- a/lib/libc/stdio/freopen.c +++ b/lib/libc/stdio/freopen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: freopen.c,v 1.18 2024/08/12 20:53:09 guenther Exp $ */ +/* $OpenBSD: freopen.c,v 1.19 2024/08/30 03:44:48 guenther Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -75,8 +75,9 @@ freopen(const char *file, const char *mode, FILE *fp) isopen = 0; wantfd = -1; } else { - /* flush the stream; POSIX, not ANSI, requires this. */ - (void) __sflush(fp); + /* flush the stream; ANSI doesn't require this. */ + if (fp->_flags & __SWR) + (void) __sflush(fp); /* if close is NULL, closing is a no-op, hence pointless */ isopen = fp->_close != NULL; if ((wantfd = fp->_file) < 0 && isopen) { diff --git a/lib/libc/stdio/fseek.c b/lib/libc/stdio/fseek.c index a65867d3f46..82a0b4be531 100644 --- a/lib/libc/stdio/fseek.c +++ b/lib/libc/stdio/fseek.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fseek.c,v 1.15 2024/08/12 20:53:09 guenther Exp $ */ +/* $OpenBSD: fseek.c,v 1.16 2024/08/30 03:44:48 guenther Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -116,8 +116,7 @@ fseeko(FILE *fp, off_t offset, int whence) /* * Can only optimise if: * reading (and not reading-and-writing); - * not unbuffered; - * not immediately after an fflush(); and + * not unbuffered; and * this is a `regular' Unix file (and hence seekfn==__sseek). * We must check __NBF first, because it is possible to have __NBF * and __SOPT both set. @@ -126,8 +125,6 @@ fseeko(FILE *fp, off_t offset, int whence) __smakebuf(fp); if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) goto dumb; - if (fp->_r == 0 && (fp->_p == NULL || fp->_p == fp->_bf._base)) - goto dumb; if ((fp->_flags & __SOPT) == 0) { if (seekfn != __sseek || fp->_file < 0 || fstat(fp->_file, &st) == -1 || @@ -231,7 +228,7 @@ fseeko(FILE *fp, off_t offset, int whence) * do it. Allow the seek function to change fp->_bf._base. */ dumb: - if (((fp->_flags & __SWR) && __sflush(fp)) || + if (__sflush(fp) || (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) { FUNLOCKFILE(fp); return (EOF); diff --git a/lib/libc/stdio/ftell.c b/lib/libc/stdio/ftell.c index 69e1f0d6ef9..743e70a86fb 100644 --- a/lib/libc/stdio/ftell.c +++ b/lib/libc/stdio/ftell.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ftell.c,v 1.12 2024/08/12 20:53:09 guenther Exp $ */ +/* $OpenBSD: ftell.c,v 1.13 2024/08/30 03:44:48 guenther Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -55,8 +55,7 @@ ftello(FILE *fp) * adjust for buffered bytes. */ FLOCKFILE(fp); - if (fp->_flags & __SWR) - __sflush(fp); /* may adjust seek offset on append stream */ + __sflush(fp); /* may adjust seek offset on append stream */ if (fp->_flags & __SOFF) pos = fp->_offset; else { diff --git a/lib/libc/stdlib/exit.3 b/lib/libc/stdlib/exit.3 index 5e006e53f40..22acade86cc 100644 --- a/lib/libc/stdlib/exit.3 +++ b/lib/libc/stdlib/exit.3 @@ -29,9 +29,9 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: exit.3,v 1.17 2024/08/12 20:53:09 guenther Exp $ +.\" $OpenBSD: exit.3,v 1.18 2024/08/30 03:44:48 guenther Exp $ .\" -.Dd $Mdocdate: August 12 2024 $ +.Dd $Mdocdate: August 30 2024 $ .Dt EXIT 3 .Os .Sh NAME @@ -54,7 +54,9 @@ Call the functions registered with the .Xr atexit 3 function, in the reverse order of their registration. .It -Flush and close all open streams. +Flush all open output streams. +.It +Close all open streams. .It Unlink all files created with the .Xr tmpfile 3 @@ -77,7 +79,6 @@ function never returns. .Sh SEE ALSO .Xr _exit 2 , .Xr atexit 3 , -.Xr fflush 3 , .Xr intro 3 , .Xr sysexits 3 , .Xr tmpfile 3 @@ -85,7 +86,7 @@ function never returns. The .Fn exit function conforms to -.St -p1003.1-2024 . +.St -isoC-99 . .Sh HISTORY An .Fn exit