Commit 48d563286c788f344dd97feba1b06da6b4b42ac0

Patrick Steinhardt 2019-06-28T10:47:37

fuzzers: implement `mkdtemp` alternative for Win32 The `mkdtemp` function is not available on Windows, so our download_refs fuzzer will fail to compile on Windows. Provide an alternative implementation to fix it.

diff --git a/fuzzers/download_refs_fuzzer.c b/fuzzers/download_refs_fuzzer.c
index 93f1b49..2b70dd0 100644
--- a/fuzzers/download_refs_fuzzer.c
+++ b/fuzzers/download_refs_fuzzer.c
@@ -15,6 +15,7 @@
 
 #include "git2.h"
 #include "git2/sys/transport.h"
+#include "fileops.h"
 
 #define UNUSED(x) (void)(x)
 
@@ -166,10 +167,23 @@ void fuzzer_git_abort(const char *op)
 
 int LLVMFuzzerInitialize(int *argc, char ***argv)
 {
-	char tmp[] = "/tmp/git2.XXXXXX";
+#if defined(_WIN32)
+	char tmpdir[MAX_PATH], path[MAX_PATH];
 
-	UNUSED(argc);
-	UNUSED(argv);
+	if (GetTempPath((DWORD)sizeof(tmpdir), tmpdir) == 0)
+		abort();
+
+	if (GetTempFileName(tmpdir, "lg2", 1, path) == 0)
+		abort();
+
+	if (git_futils_mkdir(path, 0700, 0) < 0)
+		abort();
+#else
+	char path[] = "/tmp/git2.XXXXXX";
+
+	if (mkdtemp(path) != path)
+		abort();
+#endif
 
 	if (git_libgit2_init() < 0)
 		abort();
@@ -177,10 +191,10 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
 	if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0)
 		abort();
 
-	if (mkdtemp(tmp) != tmp)
-		abort();
+	UNUSED(argc);
+	UNUSED(argv);
 
-	if (git_repository_init(&repo, tmp, 1) < 0)
+	if (git_repository_init(&repo, path, 1) < 0)
 		fuzzer_git_abort("git_repository_init");
 
 	return 0;