[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
read() returns positive numbers in BSD systems, which make: bash / valid
From: |
Eduardo A . Bustamante López |
Subject: |
read() returns positive numbers in BSD systems, which make: bash / valid. |
Date: |
Fri, 17 Apr 2015 13:43:43 -0500 |
User-agent: |
Mutt/1.5.23 (2014-03-12) |
In the BSDs, doing a read on a directory doesn't return a negative value.
Bash assumes that you can't do reads on directories here:
1501 /* Only do this with non-tty file descriptors we can seek on. */
1502 if (fd_is_tty == 0 && (lseek (fd, 0L, 1) != -1))
1503 {
1504 /* Check to see if the `file' in `bash file' is a binary file
1505 according to the same tests done by execute_simple_command (),
1506 and report an error and exit if it is. */
1507 sample_len = read (fd, sample, sizeof (sample));
1508 if (sample_len < 0)
1509 {
1510 e = errno;
1511 if ((fstat (fd, &sb) == 0) && S_ISDIR (sb.st_mode))
1512 internal_error (_("%s: is a directory"), filename);
1513 else
1514 {
1515 errno = e;
1516 file_error (filename);
1517 }
"shell.c" 1925 lines --78%--
It needs to check if the argument passed is a directory before this part.
Please see:
Linux
dualbus@yaqui ~ % cat > readtest.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
ssize_t nbytes;
int fd;
char buffer[16];
fd = open("/", O_RDONLY);
nbytes = read(fd, buffer, 16);
printf("%d %d\n", fd, nbytes);
return 0;
}
dualbus@yaqui ~ % gcc -o readtest readtest.c
dualbus@yaqui ~ % ./readtest
3 -1
NetBSD
[dualbus@faeroes.sdf.org]$ cat readtest.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
ssize_t nbytes;
int fd;
char buffer[16];
fd = open("/", O_RDONLY);
nbytes = read(fd, buffer, 16);
printf("%d %d\n", fd, nbytes);
return 0;
}
[dualbus@faeroes.sdf.org]$ gcc -o readtest readtest.c
[dualbus@faeroes.sdf.org]$ ./readtest
3 16
execute_cmd.c doesn't seem to be affected by this, only shell.c. This breaks
one of the tests that specify that:
dualbus@yaqui ~ % bash /
/: /: is a directory
Should be returned in this case.
--
Eduardo Bustamante
https://dualbus.me/
- read() returns positive numbers in BSD systems, which make: bash / valid.,
Eduardo A . Bustamante López <=