/src/zip/fuzz/fuzz_entry.c
Line | Count | Source |
1 | | #include "zip.h" |
2 | | #include <stdint.h> |
3 | | #include <stdlib.h> |
4 | | |
5 | 3.19k | int LLVMFuzzerTestOneInput(const uint8_t *data, const size_t size) { |
6 | | /* Discard inputs larger than 1MB. */ |
7 | 3.19k | static const size_t MaxSize = 1024 * 1024; |
8 | 3.19k | if (size < 1 || size > MaxSize) { |
9 | 0 | return 0; |
10 | 0 | } |
11 | | |
12 | 3.19k | void *buf = NULL; |
13 | 3.19k | size_t bufsize = 0; |
14 | 3.19k | struct zip_t *zip = zip_stream_open((const char *)data, size, 0, 'r'); |
15 | 3.19k | if (NULL == zip) { |
16 | 1.29k | goto end; |
17 | 1.29k | } |
18 | | |
19 | 1.89k | const ssize_t zip_entries_count = zip_entries_total(zip); |
20 | | |
21 | 1.89k | if (zip_entries_count <= 0) { |
22 | 58 | goto end; |
23 | 58 | } |
24 | | |
25 | 1.83k | if (0 != zip_entry_openbyindex(zip, 0)) { |
26 | 61 | goto end; |
27 | 61 | } |
28 | | |
29 | 1.77k | zip_entry_read(zip, &buf, &bufsize); |
30 | | |
31 | 3.19k | end: |
32 | 3.19k | zip_entry_close(zip); |
33 | 3.19k | if (NULL != zip) { |
34 | 1.89k | zip_close(zip); |
35 | 1.89k | } |
36 | 3.19k | free(buf); |
37 | 3.19k | return 0; |
38 | 1.77k | } |