Open llvmbot opened 9 years ago
Because that is what a shell does when it launches a process with I/O redirect. It's quite surprising to see some rubbish after normal output, and, AFAIK, explicitly truncating stdout (from child process) is not a common practice in C/C++ programs.
Why do you expect the file to be truncated?
Extended Description
The following code snippet reproduces the problem:
include <llvm/Support/Program.h>
int main(int argc, const char argv[]) { if (argc == 1) { // Parent process char const args[] = { argv[0], "foooooo", nullptr }; llvm::StringRef out("test.txt"); const llvm::StringRef* redirects[] = { nullptr, &out, nullptr }; llvm::sys::ExecuteAndWait(argv[0], args, nullptr, redirects); args[1] = "xxx"; llvm::sys::ExecuteAndWait(argv[0], args, nullptr, redirects); } else { // Child process printf("%s\n", argv[1]); } return 0; }
Actual content of 'test.txt': xxx ooo Expected content of 'test.txt': xxx
LLVM is compiled with HAVE_POSIX_SPAWN - redirect is done in function RedirectIO_PS.
Seems that this can be fixed by simply adding O_TRUNC to opening flags in RedirectIO_PS() and RedirectIO().