/src/poppler/cpp/tests/fuzzing/fuzzer_temp_file.h
Line | Count | Source |
1 | | // Copyright 2018 Google Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // Adapter utility from fuzzer input to a temporary file, for fuzzing APIs that |
16 | | // require a file instead of an input buffer. |
17 | | |
18 | | #ifndef FUZZER_TEMP_FILE_H_ |
19 | | #define FUZZER_TEMP_FILE_H_ |
20 | | |
21 | | #include <stdint.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | #include <unistd.h> |
26 | | |
27 | | // Pure-C interface for creating and cleaning up temporary files. |
28 | | |
29 | | static char *fuzzer_get_tmpfile(const uint8_t *data, size_t size) |
30 | 7.87k | { |
31 | 7.87k | char *filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX"); |
32 | 7.87k | if (!filename_buffer) { |
33 | 0 | perror("Failed to allocate file name buffer."); |
34 | 0 | abort(); |
35 | 0 | } |
36 | 7.87k | const int file_descriptor = mkstemp(filename_buffer); |
37 | 7.87k | if (file_descriptor < 0) { |
38 | 0 | perror("Failed to make temporary file."); |
39 | 0 | abort(); |
40 | 0 | } |
41 | 7.87k | FILE *file = fdopen(file_descriptor, "wb"); |
42 | 7.87k | if (!file) { |
43 | 0 | perror("Failed to open file descriptor."); |
44 | 0 | close(file_descriptor); |
45 | 0 | abort(); |
46 | 0 | } |
47 | 7.87k | const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file); |
48 | 7.87k | if (bytes_written < size) { |
49 | 0 | close(file_descriptor); |
50 | 0 | fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)", bytes_written, size); |
51 | 0 | abort(); |
52 | 0 | } |
53 | 7.87k | fclose(file); |
54 | 7.87k | return filename_buffer; |
55 | 7.87k | } |
56 | | |
57 | | static void fuzzer_release_tmpfile(char *filename) |
58 | 7.87k | { |
59 | 7.87k | if (unlink(filename) != 0) { |
60 | 0 | perror("WARNING: Failed to delete temporary file."); |
61 | 0 | } |
62 | 7.87k | free(filename); |
63 | 7.87k | } |
64 | | |
65 | | // C++ RAII object for creating temporary files. |
66 | | |
67 | | #ifdef __cplusplus |
68 | | class FuzzerTemporaryFile |
69 | | { |
70 | | public: |
71 | 0 | FuzzerTemporaryFile(const uint8_t *data, size_t size) : filename_(fuzzer_get_tmpfile(data, size)) { } |
72 | | |
73 | 0 | ~FuzzerTemporaryFile() { fuzzer_release_tmpfile(filename_); } |
74 | | |
75 | 0 | const char *filename() const { return filename_; } |
76 | | |
77 | | private: |
78 | | char *filename_; |
79 | | }; |
80 | | #endif |
81 | | |
82 | | #endif // FUZZER_TEMP_FILE_H_ |