From cd62a92d4a964bfe61d35ba2301b69e65e22a509 Mon Sep 17 00:00:00 2001 From: Robert Mustacchi Date: Mon, 2 Mar 2020 05:43:45 +0000 Subject: 7092 Want support for stdio memory streams 12360 fwrite can loop forever on zero byte write 12392 ftello64 doesn't handle ungetc() correctly when unbuffered Reviewed by: John Levon Reviewed by: Yuri Pankov Approved by: Dan McDonald --- usr/src/lib/libc/port/stdio/fopen.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'usr/src/lib/libc/port/stdio/fopen.c') diff --git a/usr/src/lib/libc/port/stdio/fopen.c b/usr/src/lib/libc/port/stdio/fopen.c index eea8093aca..728a0f4978 100644 --- a/usr/src/lib/libc/port/stdio/fopen.c +++ b/usr/src/lib/libc/port/stdio/fopen.c @@ -25,7 +25,7 @@ */ /* Copyright (c) 1988 AT&T */ -/* All Rights Reserved */ +/* All Rights Reserved */ /* * University Copyright- Copyright (c) 1982, 1986, 1988 @@ -37,7 +37,9 @@ * contributors. */ -#pragma ident "%Z%%M% %I% %E% SMI" +/* + * Copyright 2020 Robert Mustacchi + */ #include "lint.h" #include "file64.h" @@ -88,6 +90,17 @@ _freopen_null(const char *type, FILE *iop) return (NULL); } + /* + * If this is not a file-based stream (as in we have no file + * descriptor), then we need to close this, but still actually return an + * error. + */ + if (_get_fd(iop) == -1) { + (void) close_fd(iop); + errno = EBADF; + return (NULL); + } + if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW))) (void) _fflush_u(iop); @@ -140,7 +153,7 @@ _freopen_null(const char *type, FILE *iop) } #ifdef _LP64 - iop->_flag &= ~0377; /* clear lower 8-bits */ + iop->_flag &= ~_DEF_FLAG_MASK; /* clear lower 8-bits */ if (mode == 'r') { iop->_flag |= _IOREAD; nflag = oflag & ~O_APPEND; -- cgit v1.2.3