Skip to content

[libc] No need to use recursion in fcntl #99893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2024

Conversation

mikhailramalho
Copy link
Member

This patch removes the recursion in fcntl introduced by PR 99675 as it is not required and may be dangerous in some cases: there are toolchains that define F_GETLK == F_GETLK64, causing infinite recursion.

This patch removes the recursion in fcntl introduced by PR 99675 as it
is not required and may be dangerous in some cases: there are toolchains
that define F_GETLK == F_GETLK64, causing infinite recursion.
@llvmbot
Copy link
Member

llvmbot commented Jul 22, 2024

@llvm/pr-subscribers-libc

Author: Mikhail R. Gadelha (mikhailramalho)

Changes

This patch removes the recursion in fcntl introduced by PR 99675 as it is not required and may be dangerous in some cases: there are toolchains that define F_GETLK == F_GETLK64, causing infinite recursion.


Full diff: https://github.com/llvm/llvm-project/pull/99893.diff

1 Files Affected:

  • (modified) libc/src/__support/OSUtil/linux/fcntl.cpp (+10-8)
diff --git a/libc/src/__support/OSUtil/linux/fcntl.cpp b/libc/src/__support/OSUtil/linux/fcntl.cpp
index 7731aa04a178d..4742b2a00220b 100644
--- a/libc/src/__support/OSUtil/linux/fcntl.cpp
+++ b/libc/src/__support/OSUtil/linux/fcntl.cpp
@@ -33,7 +33,8 @@ int fcntl(int fd, int cmd, void *arg) {
 #error "fcntl and fcntl64 syscalls not available."
 #endif
 
-  switch (cmd) {
+  int new_cmd = cmd;
+  switch (new_cmd) {
   case F_OFD_SETLKW: {
     struct flock *flk = reinterpret_cast<struct flock *>(arg);
     // convert the struct to a flock64
@@ -44,7 +45,8 @@ int fcntl(int fd, int cmd, void *arg) {
     flk64.l_len = flk->l_len;
     flk64.l_pid = flk->l_pid;
     // create a syscall
-    return LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
+    return LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, new_cmd,
+                                             &flk64);
   }
   case F_OFD_GETLK:
   case F_OFD_SETLK: {
@@ -57,8 +59,8 @@ int fcntl(int fd, int cmd, void *arg) {
     flk64.l_len = flk->l_len;
     flk64.l_pid = flk->l_pid;
     // create a syscall
-    int retVal =
-        LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
+    int retVal = LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd,
+                                                   new_cmd, &flk64);
     // On failure, return
     if (retVal == -1)
       return -1;
@@ -89,22 +91,22 @@ int fcntl(int fd, int cmd, void *arg) {
 #ifdef SYS_fcntl64
   case F_GETLK: {
     if constexpr (FCNTL_SYSCALL_ID == SYS_fcntl64)
-      return fcntl(fd, F_GETLK64, arg);
+      new_cmd = F_GETLK64;
     break;
   }
   case F_SETLK: {
     if constexpr (FCNTL_SYSCALL_ID == SYS_fcntl64)
-      return fcntl(fd, F_SETLK64, arg);
+      new_cmd = F_SETLK64;
     break;
   }
   case F_SETLKW: {
     if constexpr (FCNTL_SYSCALL_ID == SYS_fcntl64)
-      return fcntl(fd, F_SETLKW64, arg);
+      new_cmd = F_SETLKW64;
     break;
   }
 #endif
   }
-  int retVal = LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd,
+  int retVal = LIBC_NAMESPACE::syscall_impl<int>(FCNTL_SYSCALL_ID, fd, new_cmd,
                                                  reinterpret_cast<void *>(arg));
   if (retVal >= 0) {
     return retVal;

@mikhailramalho mikhailramalho merged commit c80b799 into llvm:main Jul 22, 2024
6 of 7 checks passed
@mikhailramalho mikhailramalho deleted the no-rec-fcntl branch July 22, 2024 16:40
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants