diff options
author | Boyuan Yang <byang@debian.org> | 2023-11-27 22:46:29 -0500 |
---|---|---|
committer | Boyuan Yang <byang@debian.org> | 2023-11-27 22:46:29 -0500 |
commit | 19564cb4f77660cdb2f980ca619d4b979b9fe342 (patch) | |
tree | ff97ccd1471553f1e861c8ea747faa45a023e119 /tests/fuzzer/fuzzer_temp_file.h | |
parent | d4dbf19f6b0181ee78034bfe4caf189d1c016998 (diff) | |
download | libgav1-19564cb4f77660cdb2f980ca619d4b979b9fe342.tar.gz libgav1-19564cb4f77660cdb2f980ca619d4b979b9fe342.tar.bz2 libgav1-19564cb4f77660cdb2f980ca619d4b979b9fe342.zip |
New upstream version 0.19.0
Diffstat (limited to 'tests/fuzzer/fuzzer_temp_file.h')
-rw-r--r-- | tests/fuzzer/fuzzer_temp_file.h | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/tests/fuzzer/fuzzer_temp_file.h b/tests/fuzzer/fuzzer_temp_file.h index 5d12bbe..ed8f51c 100644 --- a/tests/fuzzer/fuzzer_temp_file.h +++ b/tests/fuzzer/fuzzer_temp_file.h @@ -25,12 +25,52 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#ifdef _WIN32 +#include <io.h> +#include <windows.h> + +#define strdup _strdup +#define unlink _unlink +#else #include <unistd.h> +#endif // _WIN32 // Pure-C interface for creating and cleaning up temporary files. static char* fuzzer_get_tmpfile_with_suffix(const uint8_t* data, size_t size, const char* suffix) { +#ifdef _WIN32 + // GetTempPathA generates '<path>\<pre><uuuu>.TMP'. + (void)suffix; // NOLINT (this could be a C compilation unit) + char temp_path[MAX_PATH]; + const DWORD ret = GetTempPathA(MAX_PATH, temp_path); + if (ret == 0 || ret > MAX_PATH) { + fprintf(stderr, "Error getting temporary directory name: %lu\n", + GetLastError()); + abort(); + } + char* filename_buffer = + (char*)malloc(MAX_PATH); // NOLINT (this could be a C compilation unit) + if (!filename_buffer) { + perror("Failed to allocate file name buffer."); + abort(); + } + if (GetTempFileNameA(temp_path, "ftf", /*uUnique=*/0, filename_buffer) == 0) { + fprintf(stderr, "Error getting temporary file name: %lu\n", GetLastError()); + abort(); + } +#if defined(_MSC_VER) || defined(MINGW_HAS_SECURE_API) + FILE* file; + const errno_t err = fopen_s(&file, filename_buffer, "wb"); + if (err != 0) file = NULL; // NOLINT (this could be a C compilation unit) +#else + FILE* file = fopen(filename_buffer, "wb"); +#endif + if (!file) { + perror("Failed to open file."); + abort(); + } +#else // !_WIN32 if (suffix == NULL) { // NOLINT (this could be a C compilation unit) suffix = ""; } @@ -55,7 +95,7 @@ static char* fuzzer_get_tmpfile_with_suffix(const uint8_t* data, size_t size, } if (snprintf(filename_buffer, buffer_sz, "%s%s", leading_temp_path, suffix) >= - buffer_sz) { + (int)buffer_sz) { // NOLINT (this could be a C compilation unit) perror("File name buffer too short."); abort(); } @@ -71,9 +111,10 @@ static char* fuzzer_get_tmpfile_with_suffix(const uint8_t* data, size_t size, close(file_descriptor); abort(); } +#endif // _WIN32 const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); if (bytes_written < size) { - close(file_descriptor); + fclose(file); fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); abort(); |