util/nvmutil: restore errno if lseek resets it

if it resets it on success, that is!

theoretically possible. we must preserve errno.

normally i'm a bit more casual about it, but this
function is replicating libc, so i must be strict

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-10 11:06:31 +00:00
parent 8d156bcf35
commit 632c85ce1c

View File

@@ -1371,6 +1371,7 @@ prw(int fd, void *mem, size_t count,
{
off_t old;
ssize_t r;
int restore_errno;
int saved_errno = 0;
if ((old = lseek_eintr(fd, (off_t)0, SEEK_CUR)) == (off_t)-1)
@@ -1391,9 +1392,13 @@ prw(int fd, void *mem, size_t count,
if (r < 0)
saved_errno = errno;
restore_errno = errno;
if (lseek_eintr(fd, old, SEEK_SET) == (off_t)-1) {
if (saved_errno)
errno = saved_errno;
else
errno = restore_errno;
return -1;
}