[llvm] r264230 - [libFuzzer] use fdopen+vfprintf instead of fsnprintf+write

Kostya Serebryany via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 23 17:57:32 PDT 2016


Author: kcc
Date: Wed Mar 23 19:57:32 2016
New Revision: 264230

URL: http://llvm.org/viewvc/llvm-project?rev=264230&view=rev
Log:
[libFuzzer] use fdopen+vfprintf instead of fsnprintf+write

Modified:
    llvm/trunk/lib/Fuzzer/FuzzerIO.cpp

Modified: llvm/trunk/lib/Fuzzer/FuzzerIO.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerIO.cpp?rev=264230&r1=264229&r2=264230&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerIO.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerIO.cpp Wed Mar 23 19:57:32 2016
@@ -20,7 +20,7 @@
 
 namespace fuzzer {
 
-static int OutputFd = 2;
+static FILE *OutputFile = stderr;
 
 bool IsFile(const std::string &Path) {
   struct stat St;
@@ -117,24 +117,23 @@ std::string DirPlusFile(const std::strin
 }
 
 void DupAndCloseStderr() {
-  assert(OutputFd == 2);
-  OutputFd = dup(OutputFd);
-  if (OutputFd < 0)
-    OutputFd = 2;
-  else
-    close(2);
+  int OutputFd = dup(2);
+  if (OutputFd > 0) {
+    FILE *NewOutputFile = fdopen(OutputFd, "w");
+    if (NewOutputFile) {
+      OutputFile = NewOutputFile;
+      close(2);
+    }
+  }
 }
 
 void CloseStdout() { close(1); }
 
 void Printf(const char *Fmt, ...) {
-  char Buf[1024];
   va_list ap;
   va_start(ap, Fmt);
-  int Formatted = vsnprintf(Buf, sizeof(Buf), Fmt, ap);
+  vfprintf(OutputFile, Fmt, ap);
   va_end(ap);
-  if (Formatted)
-    write(OutputFd, Buf, Formatted);
 }
 
 }  // namespace fuzzer




More information about the llvm-commits mailing list