diff options
author | Boyuan Yang <byang@debian.org> | 2023-11-27 22:46:32 -0500 |
---|---|---|
committer | Boyuan Yang <byang@debian.org> | 2023-11-27 22:46:32 -0500 |
commit | 7fee0fd1b17b4f963fa1db74c3a5fcc3ff142e0d (patch) | |
tree | 4ed468528001d8e80e3be09413ae927ca2ac05ce /tests/fuzzer/fuzzer_temp_file.h | |
parent | 0d1e75e423265689dd49c7d6023d8bba70ca4d05 (diff) | |
parent | 19564cb4f77660cdb2f980ca619d4b979b9fe342 (diff) | |
download | libgav1-7fee0fd1b17b4f963fa1db74c3a5fcc3ff142e0d.tar.gz libgav1-7fee0fd1b17b4f963fa1db74c3a5fcc3ff142e0d.tar.bz2 libgav1-7fee0fd1b17b4f963fa1db74c3a5fcc3ff142e0d.zip |
Update upstream source from tag 'upstream/0.19.0'
Update to upstream version '0.19.0'
with Debian dir a4233a4a247b06e8d6e36d07d059f03582d97721
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(); |