From e8d59f9c2b51859f214a84a47583e0fae5aab5b8 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Thu, 12 Sep 2024 23:06:13 +1000 Subject: fix windows pread and pwrite bug reading without a byte offset specified can affect absolute reads afterward, this is a workaround (thanks windows...) --- source/core/StarFile_windows.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'source/core/StarFile_windows.cpp') diff --git a/source/core/StarFile_windows.cpp b/source/core/StarFile_windows.cpp index 0b5c286..d9719b1 100644 --- a/source/core/StarFile_windows.cpp +++ b/source/core/StarFile_windows.cpp @@ -377,7 +377,12 @@ size_t File::pread(void* f, char* data, size_t len, StreamOffset position) { HANDLE file = (HANDLE)f; DWORD numRead = 0; OVERLAPPED overlapped = makeOverlapped(position); + + StreamOffset pos = ftell(f); + if (pos != 0) fseek(f, 0, IOSeek::Absolute); int ret = ReadFile(file, data, len, &numRead, &overlapped); + if (pos != 0) fseek(f, pos, IOSeek::Absolute); + if (ret == 0) { auto err = GetLastError(); if (err != ERROR_IO_PENDING) @@ -391,7 +396,12 @@ size_t File::pwrite(void* f, char const* data, size_t len, StreamOffset position HANDLE file = (HANDLE)f; DWORD numWritten = 0; OVERLAPPED overlapped = makeOverlapped(position); + + StreamOffset pos = ftell(f); + if (pos != 0) fseek(f, 0, IOSeek::Absolute); int ret = WriteFile(file, data, len, &numWritten, &overlapped); + if (pos != 0) fseek(f, pos, IOSeek::Absolute); + if (ret == 0) { auto err = GetLastError(); if (err != ERROR_IO_PENDING) -- cgit v1.2.3