From 63dbf5c61433dec1b907e869ac370da2b56f3ecc Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 4 Nov 2023 18:41:39 -0700 Subject: [PATCH] [wpiutil] MemoryBuffer: Fix normal read and file type check (#5875) --- .../thirdparty/llvm/cpp/llvm/MemoryBuffer.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/MemoryBuffer.cpp b/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/MemoryBuffer.cpp index 72cdf09882..3ee53a2d83 100644 --- a/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/MemoryBuffer.cpp +++ b/wpiutil/src/main/native/thirdparty/llvm/cpp/llvm/MemoryBuffer.cpp @@ -230,6 +230,7 @@ static std::unique_ptr GetMemoryBufferForStream( fs::file_t f, std::string_view bufferName, std::error_code& ec) { constexpr size_t ChunkSize = 4096 * 4; SmallVector buffer; + uint64_t size = 0; #ifdef _WIN32 DWORD readBytes; #else @@ -237,23 +238,23 @@ static std::unique_ptr GetMemoryBufferForStream( #endif // Read into Buffer until we hit EOF. do { - size_t prevSize = buffer.size(); - buffer.resize_for_overwrite(prevSize + ChunkSize); + buffer.resize_for_overwrite(size + ChunkSize); #ifdef _WIN32 - if (!ReadFile(f, buffer.begin() + prevSize, ChunkSize, &readBytes, - nullptr)) { + if (!ReadFile(f, buffer.begin() + size, ChunkSize, &readBytes, nullptr)) { ec = mapWindowsError(GetLastError()); return nullptr; } #else - readBytes = sys::RetryAfterSignal(-1, ::read, f, buffer.begin() + prevSize, + readBytes = sys::RetryAfterSignal(-1, ::read, f, buffer.begin() + size, ChunkSize); if (readBytes == -1) { ec = std::error_code(errno, std::generic_category()); return nullptr; } #endif + size += readBytes; } while (readBytes != 0); + buffer.truncate(size); return GetMemBufferCopyImpl(buffer, bufferName, ec); } @@ -370,7 +371,7 @@ static std::unique_ptr GetReadWriteFile( // If this not a file or a block device (e.g. it's a named pipe // or character device), we can't mmap it, so error out. - if (status.st_mode != S_IFREG && status.st_mode != S_IFBLK) { + if (!S_ISREG(status.st_mode) && !S_ISBLK(status.st_mode)) { ec = make_error_code(errc::invalid_argument); return nullptr; } @@ -433,7 +434,7 @@ static std::unique_ptr GetOpenFileImpl(fs::file_t f, // If this not a file or a block device (e.g. it's a named pipe // or character device), we can't trust the size. Create the memory // buffer by copying off the stream. - if (status.st_mode != S_IFREG && status.st_mode != S_IFBLK) { + if (!S_ISREG(status.st_mode) && !S_ISBLK(status.st_mode)) { return GetMemoryBufferForStream(f, filename, ec); }