Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

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

Open Quuxplusone opened 9 years ago

Quuxplusone commented 9 years ago
Bugzilla Link PR22659
Status NEW
Importance P normal
Reported by Nickolas Pokhylets (pohilets@gmail.com)
Reported on 2015-02-21 15:20:37 -0800
Last modified on 2015-02-22 13:02:00 -0800
Version 3.6
Hardware PC MacOS X
CC david.majnemer@gmail.com, llvm-bugs@lists.llvm.org, rafael@espindo.la
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
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().
Quuxplusone commented 9 years ago

Why do you expect the file to be truncated?

Quuxplusone 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.