From 881b987934d75f88eeb4fa8dd24308a881768931 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Fri, 17 Sep 2021 20:48:58 -0700 Subject: [PATCH] Explicitly error when reading directories FreeBSD will allow read() on arbitrary directories, causing fish to produce a nonsense error. Use fstat() to check for directories before reading. --- src/reader.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/reader.cpp b/src/reader.cpp index aa55d160b..17a9953ff 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -4204,8 +4204,22 @@ void reader_set_buffer(const wcstring &b, size_t pos) { /// highlighting. This is used for reading scripts and init files. /// The file is not closed. static int read_ni(parser_t &parser, int fd, const io_chain_t &io) { + struct stat buf {}; + if (fstat(fd, &buf) == -1) { + int err = errno; + FLOGF(error, _(L"Unable to read input file: %s"), strerror(err)); + return 1; + } + + /* FreeBSD allows read() on directories. Error explicitly in that case. */ + if (buf.st_mode & S_IFDIR) { + FLOGF(error, _(L"Unable to read input file: %s"), strerror(EISDIR)); + return 1; + } + // Read all data into a std::string. std::string fd_contents; + fd_contents.reserve(buf.st_size); for (;;) { char buff[4096]; ssize_t amt = read(fd, buff, sizeof buff);