Skip to content

Commit c80b799

Browse files
[libc] No need to use recursion in fcntl (#99893)
This patch removes the recursion in fcntl introduced by PR #99675 as it is not required and may be dangerous in some cases: some toolchains define F_GETLK == F_GETLK64 causing infinite recursion.
1 parent 70843bf commit c80b799

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

libc/src/__support/OSUtil/linux/fcntl.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ int fcntl(int fd, int cmd, void *arg) {
3333
#error "fcntl and fcntl64 syscalls not available."
3434
#endif
3535

36-
switch (cmd) {
36+
int new_cmd = cmd;
37+
switch (new_cmd) {
3738
case F_OFD_SETLKW: {
3839
struct flock *flk = reinterpret_cast<struct flock *>(arg);
3940
// convert the struct to a flock64
@@ -44,7 +45,8 @@ int fcntl(int fd, int cmd, void *arg) {
4445
flk64.l_len = flk->l_len;
4546
flk64.l_pid = flk->l_pid;
4647
// create a syscall
47-
return LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
48+
return LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, new_cmd,
49+
&flk64);
4850
}
4951
case F_OFD_GETLK:
5052
case F_OFD_SETLK: {
@@ -57,8 +59,8 @@ int fcntl(int fd, int cmd, void *arg) {
5759
flk64.l_len = flk->l_len;
5860
flk64.l_pid = flk->l_pid;
5961
// create a syscall
60-
int retVal =
61-
LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
62+
int retVal = LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd,
63+
new_cmd, &flk64);
6264
// On failure, return
6365
if (retVal == -1)
6466
return -1;
@@ -89,22 +91,22 @@ int fcntl(int fd, int cmd, void *arg) {
8991
#ifdef SYS_fcntl64
9092
case F_GETLK: {
9193
if constexpr (FCNTL_SYSCALL_ID == SYS_fcntl64)
92-
return fcntl(fd, F_GETLK64, arg);
94+
new_cmd = F_GETLK64;
9395
break;
9496
}
9597
case F_SETLK: {
9698
if constexpr (FCNTL_SYSCALL_ID == SYS_fcntl64)
97-
return fcntl(fd, F_SETLK64, arg);
99+
new_cmd = F_SETLK64;
98100
break;
99101
}
100102
case F_SETLKW: {
101103
if constexpr (FCNTL_SYSCALL_ID == SYS_fcntl64)
102-
return fcntl(fd, F_SETLKW64, arg);
104+
new_cmd = F_SETLKW64;
103105
break;
104106
}
105107
#endif
106108
}
107-
int retVal = LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd,
109+
int retVal = LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, new_cmd,
108110
reinterpret_cast<void *>(arg));
109111
if (retVal >= 0) {
110112
return retVal;

0 commit comments

Comments
 (0)