Coverage Report

Created: 2026-08-13 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/miniz/tests/add_in_place_fuzzer.c
Line
Count
Source
1
#include <stdio.h>
2
#include <limits.h>
3
#include "miniz.h"
4
5
static const mz_uint files_count = 5;
6
static const mz_uint max_file_size = 1024 * 1024;
7
static const char *zip_file_name = "/tmp/miniz-fuzzer-test.zip";
8
9
/* Read 32-bit integer from the fuzzer input with range [0, max] */
10
static mz_uint read_uint32(const mz_uint8 **data, size_t *size, mz_uint max)
11
0
{
12
0
    mz_uint value = 0;
13
14
0
    if (*size >= sizeof(mz_uint))
15
0
    {
16
0
        memcpy(&value, *data, sizeof(mz_uint));
17
0
        *data += sizeof(mz_uint);
18
0
        *size -= sizeof(mz_uint);
19
0
        value = MZ_MIN(max == UINT_MAX ? value : value % (max + 1), *size);
20
0
    }
21
22
0
    return value;
23
0
}
24
25
/* Read random-length null terminated string from the fuzzer input */
26
static mz_bool read_string(const mz_uint8 **data, size_t *size, char *destination, mz_uint max_len)
27
0
{
28
0
    mz_uint filename_len = read_uint32(data, size, max_len - 1);
29
0
    memcpy(destination, *data, filename_len);
30
0
    destination[filename_len] = 0;
31
0
    *data += filename_len;
32
0
    *size -= filename_len;
33
0
    return filename_len > 0;
34
0
}
35
36
/* Get random-length buffer from the fuzzer input */
37
static mz_bool read_buffer(const mz_uint8 **data, size_t *size, const mz_uint8 **destination, mz_uint *len)
38
0
{
39
0
    *len = read_uint32(data, size, max_file_size);
40
0
    *destination = *data;
41
0
    *data += *len;
42
0
    *size -= *len;
43
0
    return *len > 0;
44
0
}
45
46
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
47
{
48
    mz_uint i;
49
    char archive_file_name[FILENAME_MAX];
50
    const mz_uint8 *file_data;
51
    mz_uint file_length, flags;
52
    size_t extracted_size;
53
    mz_uint8 *extracted_data;
54
    const char *comment = mz_version();
55
56
    /* Remove the temporary file for better reproducibility */
57
    remove(zip_file_name);
58
59
    for (i = 0; i < files_count; ++i)
60
    {
61
        /* Fill archive file name */
62
        if (!read_string(&data, &size, archive_file_name, sizeof(archive_file_name)))
63
        {
64
            break;
65
        }
66
67
        /* Prepare file's content */
68
        if (!read_buffer(&data, &size, &file_data, &file_length))
69
        {
70
            break;
71
        }
72
73
        /* Prepare flags for adding file */
74
        flags = read_uint32(&data, &size, UINT_MAX);
75
76
        mz_zip_add_mem_to_archive_file_in_place(zip_file_name, archive_file_name, file_data, file_length, comment,
77
                                                (mz_uint16)strlen(comment), flags);
78
79
        /* Prepare flags for extracting file */
80
        flags = read_uint32(&data, &size, UINT_MAX);
81
        extracted_data = mz_zip_extract_archive_file_to_heap(zip_file_name, archive_file_name, &extracted_size, flags);
82
        free(extracted_data);
83
    }
84
85
    remove(zip_file_name);
86
    return 0;
87
}