mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-29 06:13:20 +00:00
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.
This commit is contained in:
parent
9a2482557d
commit
881b987934
1 changed files with 14 additions and 0 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue