Skip to content

Commit 6308cd8

Browse files
authored
[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.
1 parent 7cc1243 commit 6308cd8

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ namespace internal {
2525
LIBC_INLINE ErrorOr<off_t> lseekimpl(int fd, off_t offset, int whence) {
2626
off_t result;
2727
#ifdef SYS_lseek
28-
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_lseek, fd, offset, whence);
29-
result = ret;
28+
result = LIBC_NAMESPACE::syscall_impl<off_t>(SYS_lseek, fd, offset, whence);
29+
if (result < 0)
30+
return Error(-static_cast<int>(result));
3031
#elif defined(SYS_llseek) || defined(SYS__llseek)
3132
static_assert(sizeof(size_t) == 4, "size_t must be 32 bits.");
3233
#ifdef SYS_llseek
@@ -37,11 +38,11 @@ LIBC_INLINE ErrorOr<off_t> lseekimpl(int fd, off_t offset, int whence) {
3738
off_t offset_64 = offset;
3839
int ret = LIBC_NAMESPACE::syscall_impl<int>(
3940
LLSEEK_SYSCALL_NO, fd, offset_64 >> 32, offset_64, &result, whence);
41+
if (ret < 0)
42+
return Error(-ret);
4043
#else
4144
#error "lseek, llseek and _llseek syscalls not available."
4245
#endif
43-
if (ret < 0)
44-
return Error(-ret);
4546
return result;
4647
}
4748

0 commit comments

Comments
 (0)