Skip to content

Conversation

jtstogel
Copy link
Contributor

@jtstogel jtstogel commented Oct 7, 2025

Currently, the return value LIBC_NAMESPACE::syscall_impl<int>(SYS_lseek, fd, offset, whence) will overflow when seeking on files >4GB.

@jtstogel jtstogel requested a review from vonosmas October 7, 2025 23:15
@llvmbot llvmbot added the libc label Oct 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 7, 2025

@llvm/pr-subscribers-libc

Author: Jackson Stogel (jtstogel)

Changes

Currently, the return value LIBC_NAMESPACE::syscall_impl&lt;int&gt;(SYS_lseek, fd, offset, whence) will overflow when seeking on files >4GB.


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

1 Files Affected:

  • (modified) libc/src/__support/File/linux/lseekImpl.h (+5-4)
diff --git a/libc/src/__support/File/linux/lseekImpl.h b/libc/src/__support/File/linux/lseekImpl.h
index c22a6c59b65f1..47df99ae84b90 100644
--- a/libc/src/__support/File/linux/lseekImpl.h
+++ b/libc/src/__support/File/linux/lseekImpl.h
@@ -25,8 +25,9 @@ namespace internal {
 LIBC_INLINE ErrorOr<off_t> lseekimpl(int fd, off_t offset, int whence) {
   off_t result;
 #ifdef SYS_lseek
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_lseek, fd, offset, whence);
-  result = ret;
+  result = LIBC_NAMESPACE::syscall_impl<off_t>(SYS_lseek, fd, offset, whence);
+  if (result < 0)
+    return Error(-static_cast<int>(result));
 #elif defined(SYS_llseek) || defined(SYS__llseek)
   static_assert(sizeof(size_t) == 4, "size_t must be 32 bits.");
 #ifdef SYS_llseek
@@ -37,11 +38,11 @@ LIBC_INLINE ErrorOr<off_t> lseekimpl(int fd, off_t offset, int whence) {
   off_t offset_64 = offset;
   int ret = LIBC_NAMESPACE::syscall_impl<int>(
       LLSEEK_SYSCALL_NO, fd, offset_64 >> 32, offset_64, &result, whence);
+  if (ret < 0)
+    return Error(-ret);
 #else
 #error "lseek, llseek and _llseek syscalls not available."
 #endif
-  if (ret < 0)
-    return Error(-ret);
   return result;
 }
 

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for fixing this. Do you need me to merge this for you?

@jtstogel jtstogel enabled auto-merge (squash) October 8, 2025 17:04
@jtstogel
Copy link
Contributor Author

jtstogel commented Oct 8, 2025

LGTM, thanks for fixing this. Do you need me to merge this for you?

I have commit access now! Thanks though

@jtstogel jtstogel merged commit 6308cd8 into llvm:main Oct 8, 2025
17 of 18 checks passed
svkeerthy pushed a commit that referenced this pull request Oct 9, 2025
Currently, the return value `LIBC_NAMESPACE::syscall_impl<int>(SYS_lseek, fd, offset, whence)` will
overflow when seeking on files >4GB.
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.

4 participants