aboutsummaryrefslogtreecommitdiff
path: root/projects/opencv/fuzzer_temp_file.h
diff options
context:
space:
mode:
authorMarkus Kusano <mukusano@gmail.com>2018-12-19 19:25:05 -0500
committerjonathanmetzman <31354670+jonathanmetzman@users.noreply.github.com>2018-12-19 16:25:05 -0800
commitf9971c49145d68c3e4b5c794663349ce2fb575cf (patch)
tree7e599b42f147b8c0b1f351e3aa367ced2fbb8da7 /projects/opencv/fuzzer_temp_file.h
parent2b8d7322f0b2ac2bd24b31b33d94fea4485af548 (diff)
downloadoss-fuzz-f9971c49145d68c3e4b5c794663349ce2fb575cf.tar.gz
Integrate opencv and add two fuzz targets. (#2034)
Diffstat (limited to 'projects/opencv/fuzzer_temp_file.h')
-rw-r--r--projects/opencv/fuzzer_temp_file.h81
1 files changed, 81 insertions, 0 deletions
diff --git a/projects/opencv/fuzzer_temp_file.h b/projects/opencv/fuzzer_temp_file.h
new file mode 100644
index 000000000..fe25cabae
--- /dev/null
+++ b/projects/opencv/fuzzer_temp_file.h
@@ -0,0 +1,81 @@
+// Copyright 2018 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Adapter utility from fuzzer input to a temporary file, for fuzzing APIs that
+// require a file instead of an input buffer.
+
+#ifndef FUZZER_TEMP_FILE_H_
+#define FUZZER_TEMP_FILE_H_
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+// Pure-C interface for creating and cleaning up temporary files.
+
+static char* fuzzer_get_tmpfile(const uint8_t* data, size_t size) {
+ char* filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX");
+ if (!filename_buffer) {
+ perror("Failed to allocate file name buffer.");
+ abort();
+ }
+ const int file_descriptor = mkstemp(filename_buffer);
+ if (file_descriptor < 0) {
+ perror("Failed to make temporary file.");
+ abort();
+ }
+ FILE* file = fdopen(file_descriptor, "wb");
+ if (!file) {
+ perror("Failed to open file descriptor.");
+ close(file_descriptor);
+ abort();
+ }
+ const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file);
+ if (bytes_written < size) {
+ close(file_descriptor);
+ fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)",
+ bytes_written, size);
+ abort();
+ }
+ fclose(file);
+ return filename_buffer;
+}
+
+static void fuzzer_release_tmpfile(char* filename) {
+ if (unlink(filename) != 0) {
+ perror("WARNING: Failed to delete temporary file.");
+ }
+ free(filename);
+}
+
+// C++ RAII object for creating temporary files.
+
+#ifdef __cplusplus
+class FuzzerTemporaryFile {
+ public:
+ FuzzerTemporaryFile(const uint8_t* data, size_t size)
+ : filename_(fuzzer_get_tmpfile(data, size)) {}
+
+ ~FuzzerTemporaryFile() { fuzzer_release_tmpfile(filename_); }
+
+ const char* filename() const { return filename_; }
+
+ private:
+ char* filename_;
+};
+#endif
+
+#endif // FUZZER_TEMP_FILE_H_