From f76f04e29973354ca294034e4ff9f820731b46e5 Mon Sep 17 00:00:00 2001 From: visa Date: Tue, 11 Jan 2022 06:35:03 +0000 Subject: [PATCH] Remove dead store to f and avoid use of unvalidated fd. Found by LLVM scan-build. OK millert@ deraadt@ --- sys/lib/libsa/readdir.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sys/lib/libsa/readdir.c b/sys/lib/libsa/readdir.c index 76ff973950e..ab55059f342 100644 --- a/sys/lib/libsa/readdir.c +++ b/sys/lib/libsa/readdir.c @@ -1,4 +1,4 @@ -/* $OpenBSD: readdir.c,v 1.9 2014/11/19 20:28:56 miod Exp $ */ +/* $OpenBSD: readdir.c,v 1.10 2022/01/11 06:35:03 visa Exp $ */ /* * Copyright (c) 1996 Michael Shalayeff @@ -65,14 +65,17 @@ opendir(const char *name) int readdir(int fd, char *dest) { - struct open_file *f = &files[fd]; + struct open_file *f; - if (fd < 0 || fd >= SOPEN_MAX || - !((f = &files[fd])->f_flags & F_READ)) { + if (fd < 0 || fd >= SOPEN_MAX) { + errno = EBADF; + return (-1); + } + f = &files[fd]; + if (!(f->f_flags & F_READ)) { errno = EBADF; return (-1); } - if (f->f_flags & F_RAW) { errno = EOPNOTSUPP; return (-1); -- 2.20.1