/src/libzip/ossfuzz/zip_read_file_fuzzer.c
Line | Count | Source |
1 | | /* |
2 | | zip_random_uwp.c -- fill the user's buffer with random stuff (UWP version) |
3 | | Copyright (C) 2017-2023 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | #include <errno.h> |
34 | | #include <stdint.h> |
35 | | #include <stdio.h> |
36 | | #include <stdlib.h> |
37 | | #include <string.h> |
38 | | #include <time.h> |
39 | | #include <zip.h> |
40 | | |
41 | | #include "zip_read_fuzzer_common.h" |
42 | | |
43 | | /** |
44 | | This fuzzing target takes input data, creates a ZIP archive from it, checks the archive's consistency, |
45 | | and iterates over the entries in the archive, reading data from each entry. |
46 | | **/ |
47 | | |
48 | | #ifdef __cplusplus |
49 | | extern "C" |
50 | | #endif |
51 | | int |
52 | 4.56k | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
53 | 4.56k | zip_t *za; |
54 | 4.56k | const char *name = "test.zip"; |
55 | 4.56k | FILE *fp; |
56 | 4.56k | zip_error_t error; |
57 | 4.56k | int err = 0; |
58 | | |
59 | 4.56k | (void)remove(name); |
60 | 4.56k | if ((fp = fopen(name, "wb")) == NULL) { |
61 | 0 | fprintf(stderr, "can't create file '%s': %s\n", name, strerror(errno)); |
62 | 0 | return 0; |
63 | 0 | } |
64 | 4.56k | if (fwrite(data, 1, size, fp) != size) { |
65 | 0 | fprintf(stderr, "can't write data to file '%s': %s\n", name, strerror(errno)); |
66 | 0 | fclose(fp); |
67 | 0 | (void)remove(name); |
68 | 0 | return 0; |
69 | 0 | } |
70 | 4.56k | if (fclose(fp) < 0) { |
71 | 0 | fprintf(stderr, "can't close file '%s': %s\n", name, strerror(errno)); |
72 | 0 | (void)remove(name); |
73 | 0 | return 0; |
74 | 0 | } |
75 | | |
76 | 4.56k | za = zip_open(name, 0, &err); |
77 | 4.56k | zip_error_init_with_code(&error, err); |
78 | | |
79 | 4.56k | fuzzer_read(za, &error, NULL); |
80 | | |
81 | 4.56k | (void)remove(name); |
82 | 4.56k | return 0; |
83 | 4.56k | } |