Merge pull request #119 from randy408/fuzz
Add fuzz target to build
diff --git a/regress/CMakeLists.txt b/regress/CMakeLists.txt
index 6337e16..756a270 100644
--- a/regress/CMakeLists.txt
+++ b/regress/CMakeLists.txt
@@ -204,3 +204,6 @@
INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/../lib ${CMAKE_CURRENT_SOURCE_DIR}/../src ${CMAKE_CURRENT_BINARY_DIR}/..)
ADD_CUSTOM_TARGET(check COMMAND ${CMAKE_CTEST_COMMAND})
+
+ADD_EXECUTABLE(fuzz_main fuzz_main.c)
+TARGET_LINK_LIBRARIES(fuzz_main zip)
\ No newline at end of file
diff --git a/regress/fuzz_main.c b/regress/fuzz_main.c
new file mode 100644
index 0000000..694322e
--- /dev/null
+++ b/regress/fuzz_main.c
@@ -0,0 +1,52 @@
+#include "zip_read_fuzzer.cc"
+#include <stdio.h>
+#include <stdlib.h>
+
+/* fuzz target entry point, works without libFuzzer */
+
+int main(int argc, char **argv)
+{
+ FILE *f;
+ char *buf = NULL;
+ long siz_buf;
+
+ if(argc < 2)
+ {
+ fprintf(stderr, "no input file\n");
+ goto err;
+ }
+
+ f = fopen(argv[1], "rb");
+ if(f == NULL)
+ {
+ fprintf(stderr, "error opening input file %s\n", argv[1]);
+ goto err;
+ }
+
+ fseek(f, 0, SEEK_END);
+
+ siz_buf = ftell(f);
+ rewind(f);
+
+ if(siz_buf < 1) goto err;
+
+ buf = (char*)malloc(siz_buf);
+ if(buf == NULL)
+ {
+ fprintf(stderr, "malloc() failed\n");
+ goto err;
+ }
+
+ if(fread(buf, siz_buf, 1, f) != 1)
+ {
+ fprintf(stderr, "fread() failed\n");
+ goto err;
+ }
+
+ (void)LLVMFuzzerTestOneInput((uint8_t*)buf, siz_buf);
+
+err:
+ free(buf);
+
+ return 0;
+}
diff --git a/regress/zip_read_fuzzer.cc b/regress/zip_read_fuzzer.cc
index 22a71e9..2fb527b 100644
--- a/regress/zip_read_fuzzer.cc
+++ b/regress/zip_read_fuzzer.cc
@@ -1,7 +1,11 @@
#include "../lib/zip.h"
-extern "C" int
-LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+
+#ifdef __cplusplus
+extern "C"
+#endif
+int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
+{
zip_source_t *src;
zip_t *za;
zip_error_t error;