From e55019792fa382e1b255e9d7419dada0e1ae68a1 Mon Sep 17 00:00:00 2001 From: jb Date: Wed, 10 Jun 1998 22:28:45 +0000 Subject: Check the access mode in the flags before waiting on a read or a write that might never be possible if the file was not opened in the corrent mode. This prevents a hang for bad programs. Why do people code like that? --- lib/libpthread/thread/thr_read.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'lib/libpthread/thread/thr_read.c') diff --git a/lib/libpthread/thread/thr_read.c b/lib/libpthread/thread/thr_read.c index 358a620..6c4d211 100644 --- a/lib/libpthread/thread/thr_read.c +++ b/lib/libpthread/thread/thr_read.c @@ -29,7 +29,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: uthread_read.c,v 1.4 1998/04/29 09:59:10 jb Exp $ + * $Id: uthread_read.c,v 1.5 1998/06/09 23:20:53 jb Exp $ * */ #include @@ -45,6 +45,7 @@ ssize_t read(int fd, void *buf, size_t nbytes) { int ret; + int type; /* POSIX says to do just this: */ if (nbytes == 0) @@ -52,6 +53,17 @@ read(int fd, void *buf, size_t nbytes) /* Lock the file descriptor for read: */ if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { + /* Get the read/write mode type: */ + type = _thread_fd_table[fd]->flags & O_ACCMODE; + + /* Check if the file is not open for read: */ + if (type != O_RDONLY && type != O_RDWR) { + /* File is not open for read: */ + errno = EBADF; + _FD_UNLOCK(fd, FD_READ); + return (-1); + } + /* Perform a non-blocking read syscall: */ while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) { if ((_thread_fd_table[fd]->flags & O_NONBLOCK) == 0 && -- cgit v1.1