[wpiutil] MemoryBuffer: Fix GetMemoryBufferForStream (#5017)

This would previously just write past the end of the buffer, smashing
the stack.  It's only called in the case when a non-file or block device
is used as the file.
This commit is contained in:
Peter Johnson
2023-01-28 22:38:34 -08:00
committed by GitHub
parent 9c4c07c0f9
commit 49bb1358d8

View File

@@ -237,14 +237,17 @@ static std::unique_ptr<WritableMemoryBuffer> GetMemoryBufferForStream(
#endif
// Read into Buffer until we hit EOF.
do {
buffer.resize_for_overwrite(buffer.size() + ChunkSize);
size_t prevSize = buffer.size();
buffer.resize_for_overwrite(prevSize + ChunkSize);
#ifdef _WIN32
if (!ReadFile(f, buffer.end(), ChunkSize, &readBytes, nullptr)) {
if (!ReadFile(f, buffer.begin() + prevSize, ChunkSize, &readBytes,
nullptr)) {
ec = mapWindowsError(GetLastError());
return nullptr;
}
#else
readBytes = sys::RetryAfterSignal(-1, ::read, f, buffer.end(), ChunkSize);
readBytes = sys::RetryAfterSignal(-1, ::read, f, buffer.begin() + prevSize,
ChunkSize);
if (readBytes == -1) {
ec = std::error_code(errno, std::generic_category());
return nullptr;