llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.79k stars 11.9k forks source link

llvm::sys::ExecuteAndWait does not truncate redirected output #23033

Open llvmbot opened 9 years ago

llvmbot commented 9 years ago
Bugzilla Link 22659
Version 3.6
OS MacOS X
Reporter LLVM Bugzilla Contributor
CC @majnemer

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().

llvmbot commented 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.

991901f3-cc14-4404-b340-165691b62a58 commented 9 years ago

Why do you expect the file to be truncated?