Coverage Report

Created: 2026-02-26 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/test/fuzz/fuzzer_example_flush.c
Line
Count
Source
1
#include <stdio.h>
2
#include <assert.h>
3
4
#include "zbuild.h"
5
#ifdef ZLIB_COMPAT
6
#  include "zlib.h"
7
#else
8
#  include "zlib-ng.h"
9
#endif
10
11
29.2k
#define CHECK_ERR(err, msg) { \
12
29.2k
    if (err != Z_OK) { \
13
0
        fprintf(stderr, "%s error: %d\n", msg, err); \
14
0
        exit(1); \
15
0
    } \
16
29.2k
}
17
18
static const uint8_t *data;
19
static size_t dataLen;
20
static alloc_func zalloc = NULL;
21
static free_func zfree = NULL;
22
23
/* ===========================================================================
24
 * Test deflate() with full flush
25
 */
26
4.18k
void test_flush(unsigned char *compr, z_size_t *comprLen) {
27
4.18k
    PREFIX3(stream) c_stream; /* compression stream */
28
4.18k
    int err;
29
4.18k
    unsigned int len = (unsigned int)dataLen;
30
31
4.18k
    c_stream.zalloc = zalloc;
32
4.18k
    c_stream.zfree = zfree;
33
4.18k
    c_stream.opaque = (void *)0;
34
35
4.18k
    err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION);
36
4.18k
    CHECK_ERR(err, "deflateInit");
37
38
4.18k
    c_stream.next_in = (z_const unsigned char *)data;
39
4.18k
    c_stream.next_out = compr;
40
4.18k
    c_stream.avail_in = 3;
41
4.18k
    c_stream.avail_out = (unsigned int)*comprLen;
42
4.18k
    err = PREFIX(deflate)(&c_stream, Z_FULL_FLUSH);
43
4.18k
    CHECK_ERR(err, "deflate flush 1");
44
45
4.18k
    compr[3]++; /* force an error in first compressed block */
46
4.18k
    c_stream.avail_in = len - 3;
47
48
4.18k
    err = PREFIX(deflate)(&c_stream, Z_FINISH);
49
4.18k
    if (err != Z_STREAM_END) {
50
0
        CHECK_ERR(err, "deflate flush 2");
51
0
    }
52
4.18k
    err = PREFIX(deflateEnd)(&c_stream);
53
4.18k
    CHECK_ERR(err, "deflateEnd");
54
55
4.18k
    *comprLen = (z_size_t)c_stream.total_out;
56
4.18k
}
57
58
/* ===========================================================================
59
 * Test inflateSync()
60
 */
61
4.18k
void test_sync(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) {
62
4.18k
    int err;
63
4.18k
    PREFIX3(stream) d_stream; /* decompression stream */
64
65
4.18k
    d_stream.zalloc = zalloc;
66
4.18k
    d_stream.zfree = zfree;
67
4.18k
    d_stream.opaque = (void *)0;
68
69
4.18k
    d_stream.next_in = compr;
70
4.18k
    d_stream.avail_in = 2; /* just read the zlib header */
71
72
4.18k
    err = PREFIX(inflateInit)(&d_stream);
73
4.18k
    CHECK_ERR(err, "inflateInit");
74
75
4.18k
    d_stream.next_out = uncompr;
76
4.18k
    d_stream.avail_out = (unsigned int)uncomprLen;
77
78
4.18k
    err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH);
79
4.18k
    CHECK_ERR(err, "inflate");
80
81
4.18k
    d_stream.avail_in = (unsigned int)comprLen - 2; /* read all compressed data */
82
4.18k
    err = PREFIX(inflateSync)(&d_stream); /* but skip the damaged part */
83
4.18k
    CHECK_ERR(err, "inflateSync");
84
85
4.18k
    err = PREFIX(inflate)(&d_stream, Z_FINISH);
86
4.18k
    if (err != Z_STREAM_END) {
87
0
        fprintf(stderr, "inflate should report Z_STREAM_END\n");
88
0
        exit(1);
89
0
    }
90
4.18k
    err = PREFIX(inflateEnd)(&d_stream);
91
4.18k
    CHECK_ERR(err, "inflateEnd");
92
4.18k
}
93
94
4.18k
int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) {
95
4.18k
    z_size_t comprLen = 100 + 2 * PREFIX(compressBound)(size);
96
4.18k
    z_size_t uncomprLen = (z_size_t)size;
97
4.18k
    uint8_t *compr, *uncompr;
98
99
    /* Discard inputs larger than 1Mb. */
100
4.18k
    static size_t kMaxSize = 1024 * 1024;
101
102
    // This test requires at least 3 bytes of input data.
103
4.18k
    if (size <= 3 || size > kMaxSize)
104
3
        return 0;
105
106
4.18k
    data = d;
107
4.18k
    dataLen = size;
108
4.18k
    compr = (uint8_t *)calloc(1, comprLen);
109
4.18k
    uncompr = (uint8_t *)calloc(1, uncomprLen);
110
111
4.18k
    test_flush(compr, &comprLen);
112
4.18k
    test_sync(compr, comprLen, uncompr, uncomprLen);
113
114
4.18k
    free(compr);
115
4.18k
    free(uncompr);
116
117
    /* This function must return 0. */
118
4.18k
    return 0;
119
4.18k
}