coccinelle: always use fcntl(fd, FD_DUPFD, 3) instead of dup(fd)

Let's avoid fds 0…2 for safety reasons.
This commit is contained in:
Lennart Poettering 2018-03-20 20:57:37 +01:00
parent be6b0c2165
commit 43dc7aa2ba
2 changed files with 7 additions and 2 deletions

View File

@ -0,0 +1,5 @@
@@
expression fd;
@@
- dup(fd)
+ fcntl(fd, F_DUPFD, 3)

View File

@ -71,9 +71,9 @@ static void test_same_fd(void) {
_cleanup_close_ int a = -1, b = -1, c = -1;
assert_se(pipe2(p, O_CLOEXEC) >= 0);
assert_se((a = dup(p[0])) >= 0);
assert_se((a = fcntl(p[0], F_DUPFD, 3)) >= 0);
assert_se((b = open("/dev/null", O_RDONLY|O_CLOEXEC)) >= 0);
assert_se((c = dup(a)) >= 0);
assert_se((c = fcntl(a, F_DUPFD, 3)) >= 0);
assert_se(same_fd(p[0], p[0]) > 0);
assert_se(same_fd(p[1], p[1]) > 0);