/src/xz/tests/ossfuzz/fuzz_decode_index.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: 0BSD |
2 | | |
3 | | /////////////////////////////////////////////////////////////////////////////// |
4 | | // |
5 | | /// \file fuzz_decode_alone.c |
6 | | /// \brief Fuzz test program for .xz Index decoding |
7 | | // |
8 | | // Author: Lasse Collin |
9 | | // |
10 | | /////////////////////////////////////////////////////////////////////////////// |
11 | | |
12 | | #include <inttypes.h> |
13 | | #include <stdlib.h> |
14 | | #include <stdio.h> |
15 | | #include "lzma.h" |
16 | | #include "fuzz_common.h" |
17 | | |
18 | | |
19 | | extern int |
20 | | LLVMFuzzerTestOneInput(const uint8_t *inbuf, size_t inbuf_size) |
21 | 1.30k | { |
22 | 1.30k | lzma_stream strm = LZMA_STREAM_INIT; |
23 | 1.30k | prepare_stream(&strm, inbuf, inbuf_size); |
24 | | |
25 | 1.30k | lzma_ret ret; |
26 | | |
27 | 5.22k | for (int i = 0; i < 3; ++i) { |
28 | | // The initial pointer value must be ignored. |
29 | 3.91k | lzma_index *idx = (void *)1; |
30 | 3.91k | ret = lzma_index_decoder(&strm, &idx, MEM_LIMIT); |
31 | | |
32 | 3.91k | if (ret == LZMA_MEM_ERROR) |
33 | 23 | continue; |
34 | | |
35 | 3.89k | if (ret != LZMA_OK) { |
36 | | // This should never happen unless the system has |
37 | | // no free memory or address space to allow the small |
38 | | // allocations that the initialization requires. |
39 | 0 | fprintf(stderr, "lzma_index_decoder() failed (%d)\n", |
40 | 0 | ret); |
41 | 0 | abort(); |
42 | 0 | } |
43 | | |
44 | 3.89k | fuzz_code(&strm, strm.next_in, strm.avail_in); |
45 | 3.89k | lzma_index_end(idx, NULL); |
46 | 3.89k | } |
47 | | |
48 | 1.30k | { |
49 | | // Fuzz the single-call API too. |
50 | 1.30k | lzma_index *idx = (void *)1; |
51 | 1.30k | uint64_t memlimit = MEM_LIMIT; |
52 | 1.30k | size_t inbuf_pos = 0; |
53 | 1.30k | if (lzma_index_buffer_decode(&idx, &memlimit, NULL, |
54 | 1.30k | inbuf, &inbuf_pos, inbuf_size) |
55 | 1.30k | == LZMA_PROG_ERROR) { |
56 | 0 | fprintf(stderr, "lzma_index_buffer_decode() failed " |
57 | 0 | "with LZMA_PROG_ERROR\n"); |
58 | 0 | abort(); |
59 | 0 | } |
60 | | |
61 | 1.30k | lzma_index_end(idx, NULL); |
62 | 1.30k | } |
63 | | |
64 | 0 | lzma_end(&strm); |
65 | 1.30k | return 0; |
66 | 1.30k | } |