[llvm] [Support] Fix file creation flags used when redirecting IO (PR #90228)
Simeon K via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 26 09:12:21 PDT 2024
https://github.com/simeonkr created https://github.com/llvm/llvm-project/pull/90228
The destination file for the redirection should be truncated since otherwise the contents of any existing file may end up mixed in with the desired output.
>From 12ba1b69dac9e466b60709f281385af35588ec6c Mon Sep 17 00:00:00 2001
From: Simeon Krastnikov <simeon.krastnikov at imgtec.com>
Date: Fri, 26 Apr 2024 16:59:41 +0100
Subject: [PATCH] [Support] Fix file creation flags used when redirecting IO
The destination file for the redirection should be truncated since
otherwise the contents of any existing file may end up mixed in with the
desired output.
---
llvm/lib/Support/Unix/Program.inc | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Support/Unix/Program.inc b/llvm/lib/Support/Unix/Program.inc
index 2742734bb11ed0..9d2c4e60d358a6 100644
--- a/llvm/lib/Support/Unix/Program.inc
+++ b/llvm/lib/Support/Unix/Program.inc
@@ -107,7 +107,8 @@ static bool RedirectIO(std::optional<StringRef> Path, int FD, std::string *ErrMs
File = std::string(*Path);
// Open the file
- int InFD = open(File.c_str(), FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666);
+ int InFD = open(File.c_str(),
+ FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (InFD == -1) {
MakeErrMsg(ErrMsg, "Cannot open file '" + File + "' for " +
(FD == 0 ? "input" : "output"));
@@ -137,7 +138,8 @@ static bool RedirectIO_PS(const std::string *Path, int FD, std::string *ErrMsg,
File = Path->c_str();
if (int Err = posix_spawn_file_actions_addopen(
- FileActions, FD, File, FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666))
+ FileActions, FD, File,
+ FD == 0 ? O_RDONLY : O_WRONLY | O_CREAT | O_TRUNC, 0666))
return MakeErrMsg(ErrMsg, "Cannot posix_spawn_file_actions_addopen", Err);
return false;
}
More information about the llvm-commits
mailing list