[PATCH] D27579: [libFuzzer] Diff 19 - Automatically link with Windows library.

Marcos Pividori via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 8 11:27:08 PST 2016


mpividori updated this revision to Diff 80792.
mpividori added a comment.

@zturner @amccarth 
This is an option, using regex. It is simpler than the implementation in Support library, and probably more correct (I don't have much experience with Windows), but maybe not very efficient.
If we don't use regex, as Support library does, this will require many more lines of code. What do you think?
Thanks.


Repository:
  rL LLVM

https://reviews.llvm.org/D27579

Files:
  lib/Fuzzer/FuzzerIOWindows.cpp


Index: lib/Fuzzer/FuzzerIOWindows.cpp
===================================================================
--- lib/Fuzzer/FuzzerIOWindows.cpp
+++ lib/Fuzzer/FuzzerIOWindows.cpp
@@ -18,6 +18,7 @@
 #include <fstream>
 #include <io.h>
 #include <iterator>
+#include <regex>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <windows.h>
@@ -141,10 +142,45 @@
 }
 
 std::string DirName(const std::string &FileName) {
-  std::string Res = FileName;
-  PathRemoveFileSpecA(&Res[0]);
-  Res.resize(std::strlen(Res.c_str()));
-  return Res;
+// Location, like: \\?\UNC\Server\Share\  \\?\C:\  \\Server\Share\  \  C:\  C:
+//   ((\\\\\?\\((UNC(\\[^\\]+){2})|(.:))\\)|(\\\\([^\\]+\\){2})|(\\)|(.:\\?))?
+// Directories, like: SomeDir\AnotherDir\
+//   (([^\\]+\\)*)
+// File, like: SomeFile.txt
+//   ([^\\]+)?
+  static std::regex Reg(
+  R"(((\\\\\?\\((UNC(\\[^\\]+){2})|(.:))\\)|(\\\\([^\\]+\\){2})|(\\)|(.:\\?))?)"
+  R"((([^\\]+\\)*))"
+  R"(([^\\]+)?)");
+
+  std::smatch MatchRes;
+  if (!regex_match(FileName, MatchRes, Reg)) {
+    Printf("DirName() failed for \"%s\", invalid path.\n", FileName.c_str());
+    exit(1);
+  }
+
+  std::string Location(MatchRes[1]), Dir(MatchRes[11]), File(MatchRes[13]);
+
+  if (!Dir.empty()) {
+    Dir.pop_back(); // Remove trailing '\'.
+    if (File.empty()) { // Path ended in '\'.
+      assert(!Dir.empty());
+      // Remove file name from Dir.
+      size_t LastSep = Dir.find_last_of('\\');
+      if (LastSep == std::string::npos)
+        LastSep = 0;
+      Dir.erase(LastSep);
+    }
+  }
+
+  if (Location.empty()) { // Relative path.
+    if (Dir.empty())
+      Location = ".";
+    else
+      Location = ".\\";
+  }
+
+  return Location.append(Dir);
 }
 
 }  // namespace fuzzer


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27579.80792.patch
Type: text/x-patch
Size: 1736 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161208/f46c6725/attachment.bin>


More information about the llvm-commits mailing list