From 43f336dd00b31182cbe54da31f89298dca9c43c4 Mon Sep 17 00:00:00 2001 From: Kartik Naik Date: Wed, 15 Jul 2026 18:38:33 +0530 Subject: restore file offset on error in pread/pwrite shims --- tls/compat/pread.c | 14 +++++++++----- tls/compat/pwrite.c | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/tls/compat/pread.c b/tls/compat/pread.c index b9d6b09..0b994f4 100644 --- a/tls/compat/pread.c +++ b/tls/compat/pread.c @@ -8,21 +8,25 @@ #define NO_REDEF_POSIX_FUNCTIONS +#include #include ssize_t pread(int d, void *buf, size_t nbytes, off_t offset) { - off_t cpos, opos, rpos; + off_t cpos; ssize_t bytes; + int save_errno; + if((cpos = lseek(d, 0, SEEK_CUR)) == -1) return -1; - if((opos = lseek(d, offset, SEEK_SET)) == -1) - return -1; - if((bytes = read(d, buf, nbytes)) == -1) + if(lseek(d, offset, SEEK_SET) == -1) return -1; - if((rpos = lseek(d, cpos, SEEK_SET)) == -1) + bytes = read(d, buf, nbytes); + save_errno = errno; + if(lseek(d, cpos, SEEK_SET) == -1) return -1; + errno = save_errno; return bytes; } diff --git a/tls/compat/pwrite.c b/tls/compat/pwrite.c index 82f5f55..ab88241 100644 --- a/tls/compat/pwrite.c +++ b/tls/compat/pwrite.c @@ -8,21 +8,25 @@ #define NO_REDEF_POSIX_FUNCTIONS +#include #include ssize_t pwrite(int d, const void *buf, size_t nbytes, off_t offset) { - off_t cpos, opos, rpos; + off_t cpos; ssize_t bytes; + int save_errno; + if((cpos = lseek(d, 0, SEEK_CUR)) == -1) return -1; - if((opos = lseek(d, offset, SEEK_SET)) == -1) - return -1; - if((bytes = write(d, buf, nbytes)) == -1) + if(lseek(d, offset, SEEK_SET) == -1) return -1; - if((rpos = lseek(d, cpos, SEEK_SET)) == -1) + bytes = write(d, buf, nbytes); + save_errno = errno; + if(lseek(d, cpos, SEEK_SET) == -1) return -1; + errno = save_errno; return bytes; } -- cgit v1.2.3-55-g6feb