[llvm] [Support] Advance the buffer after partial file writes (PR #209480)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 14 06:34:26 PDT 2026
https://github.com/NotPppp1116 created https://github.com/llvm/llvm-project/pull/209480
## What
Advance both the buffer pointer and byte count while retrying partial writes in `copy_file_internal`, and treat a zero-byte write as an I/O error.
## Why
The previous loop decreased `BytesRead` after a short write but always passed the original buffer pointer to the next write. A short write therefore duplicated the buffer prefix and omitted later bytes. A zero-byte write could also loop forever.
## Impact
Descriptor-based file copies now preserve the complete buffer across partial writes and terminate cleanly on a non-progressing write.
## Checks
- built `SupportTests`
- `FileSystemTest.CopyFile` passed
- focused ReturnGuard scan reports no partial-transfer diagnostic for the corrected loop
>From ca723399911493809c85a73090c6badc7693a228 Mon Sep 17 00:00:00 2001
From: Pppp1116 <ACCOUNT1_NOREPLY>
Date: Tue, 14 Jul 2026 14:25:09 +0100
Subject: [PATCH] [Support] Advance the buffer after partial file writes
---
llvm/lib/Support/Path.cpp | 30 ++++++++++++++----------------
1 file changed, 14 insertions(+), 16 deletions(-)
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index 65e294020291f..2ca76b8ee95cc 100644
--- a/llvm/lib/Support/Path.cpp
+++ b/llvm/lib/Support/Path.cpp
@@ -1016,26 +1016,24 @@ std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
static std::error_code copy_file_internal(int ReadFD, int WriteFD) {
const size_t BufSize = 4096;
- char *Buf = new char[BufSize];
- int BytesRead = 0, BytesWritten = 0;
+ auto Buf = std::make_unique<char[]>(BufSize);
for (;;) {
- BytesRead = read(ReadFD, Buf, BufSize);
- if (BytesRead <= 0)
- break;
- while (BytesRead) {
- BytesWritten = write(WriteFD, Buf, BytesRead);
+ int BytesRead = read(ReadFD, Buf.get(), BufSize);
+ if (BytesRead < 0)
+ return errnoAsErrorCode();
+ if (BytesRead == 0)
+ return std::error_code();
+
+ int Offset = 0;
+ while (Offset < BytesRead) {
+ int BytesWritten = write(WriteFD, Buf.get() + Offset, BytesRead - Offset);
if (BytesWritten < 0)
- break;
- BytesRead -= BytesWritten;
+ return errnoAsErrorCode();
+ if (BytesWritten == 0)
+ return make_error_code(errc::io_error);
+ Offset += BytesWritten;
}
- if (BytesWritten < 0)
- break;
}
- delete[] Buf;
-
- if (BytesRead < 0 || BytesWritten < 0)
- return errnoAsErrorCode();
- return std::error_code();
}
#ifndef __APPLE__
More information about the llvm-commits
mailing list