[llvm] [Support] Fix file creation flags used when redirecting IO (PR #90228)

via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 09:12:56 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Simeon K (simeonkr)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/90228.diff


1 Files Affected:

- (modified) llvm/lib/Support/Unix/Program.inc (+4-2) 


``````````diff
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;
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/90228


More information about the llvm-commits mailing list