[libc-commits] [libc] 6308cd8 - [libc] Fix integer overflow for large offsets in lseek. (#162394)

via libc-commits libc-commits at lists.llvm.org
Wed Oct 8 10:04:57 PDT 2025


Author: Jackson Stogel
Date: 2025-10-08T17:04:52Z
New Revision: 6308cd871c955490575134e66da8a8d575674724

URL: https://github.com/llvm/llvm-project/commit/6308cd871c955490575134e66da8a8d575674724
DIFF: https://github.com/llvm/llvm-project/commit/6308cd871c955490575134e66da8a8d575674724.diff

LOG: [libc] Fix integer overflow for large offsets in lseek. (#162394)

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

Added: 
    

Modified: 
    libc/src/__support/File/linux/lseekImpl.h

Removed: 
    


################################################################################
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;
 }
 


        


More information about the libc-commits mailing list