[PATCH] D35245: [LibFuzzer] Fix GCC `-Wunused-result` warning

Dan Liew via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 11 04:05:51 PDT 2017


delcypher created this revision.

[LibFuzzer] Fix GCC `-Wunused-result` warning in
`Fuzzer::RawPrint(const char*)` and make the implementation more robust by handling partial writes.

The warning was

  ./FuzzerIOPosix.cpp:118:29: warning: ignoring return value of ‘ssize_t write(int, const void*, size_t)’, declared with attribute warn_unused_result [-Wunused-result]
     write(2, Str, strlen(Str));


https://reviews.llvm.org/D35245

Files:
  lib/Fuzzer/FuzzerIOPosix.cpp


Index: lib/Fuzzer/FuzzerIOPosix.cpp
===================================================================
--- lib/Fuzzer/FuzzerIOPosix.cpp
+++ lib/Fuzzer/FuzzerIOPosix.cpp
@@ -16,6 +16,7 @@
 #include <cstdarg>
 #include <cstdio>
 #include <dirent.h>
+#include <errno.h>
 #include <fstream>
 #include <iterator>
 #include <libgen.h>
@@ -113,9 +114,26 @@
   return true;
 }
 
-
 void RawPrint(const char *Str) {
-  write(2, Str, strlen(Str));
+  const size_t StrLength = strlen(Str);
+  size_t RemainingBytes = StrLength;
+  while (RemainingBytes > 0) {
+    ssize_t BytesWritten =
+        write(2, Str + (StrLength - RemainingBytes), RemainingBytes);
+    if (BytesWritten == -1) {
+      if (errno == EINTR) {
+        // Syscall was interrupted. Try again.
+        continue;
+      }
+      // Otherwise just give up.
+      break;
+    }
+    if (BytesWritten == 0) {
+      // Give up.
+      break;
+    }
+    RemainingBytes -= BytesWritten;
+  }
 }
 
 }  // namespace fuzzer


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35245.105997.patch
Type: text/x-patch
Size: 981 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170711/02bf3113/attachment.bin>


More information about the llvm-commits mailing list