Coverage Report

Created: 2026-12-01 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/example_small_fuzzer.c
Line
Count
Source
1
/* Copyright 2018 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
13
#include <stdio.h>
14
#include <stddef.h>
15
#include <stdint.h>
16
#include <string.h>
17
#include <assert.h>
18
#include <stdlib.h>
19
#include <inttypes.h>
20
#include "zlib.h"
21
22
362M
#define CHECK_ERR(err, msg) { \
23
362M
    if (err != Z_OK) { \
24
0
        fprintf(stderr, "%s error: %d\n", msg, err); \
25
0
        return 0; \
26
0
    } \
27
362M
}
28
29
static const uint8_t *data;
30
static size_t dataLen;
31
static alloc_func zalloc = NULL;
32
static free_func zfree = NULL;
33
34
/* ===========================================================================
35
 * Test deflate() with small buffers
36
 */
37
2.51k
int test_deflate(unsigned char *compr, size_t comprLen) {
38
2.51k
  z_stream c_stream; /* compression stream */
39
2.51k
  int err;
40
2.51k
  unsigned long len = dataLen;
41
42
2.51k
  c_stream.zalloc = zalloc;
43
2.51k
  c_stream.zfree = zfree;
44
2.51k
  c_stream.opaque = (void *)0;
45
46
2.51k
  err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
47
2.51k
  CHECK_ERR(err, "deflateInit");
48
49
2.51k
  c_stream.next_in = (Bytef *)data;
50
2.51k
  c_stream.next_out = compr;
51
52
213M
  while (c_stream.total_in != len && c_stream.total_out < comprLen) {
53
213M
    c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
54
213M
    err = deflate(&c_stream, Z_NO_FLUSH);
55
213M
    CHECK_ERR(err, "deflate small 1");
56
213M
  }
57
  /* Finish the stream, still forcing small buffers: */
58
9.67M
  for (;;) {
59
9.67M
    c_stream.avail_out = 1;
60
9.67M
    err = deflate(&c_stream, Z_FINISH);
61
9.67M
    if (err == Z_STREAM_END)
62
2.51k
      break;
63
9.66M
    CHECK_ERR(err, "deflate small 2");
64
9.66M
  }
65
66
2.51k
  err = deflateEnd(&c_stream);
67
2.51k
  CHECK_ERR(err, "deflateEnd");
68
2.51k
  return 0;
69
2.51k
}
70
71
/* ===========================================================================
72
 * Test inflate() with small buffers
73
 */
74
int test_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr,
75
2.51k
                  size_t uncomprLen) {
76
2.51k
  int err;
77
2.51k
  z_stream d_stream; /* decompression stream */
78
79
2.51k
  d_stream.zalloc = zalloc;
80
2.51k
  d_stream.zfree = zfree;
81
2.51k
  d_stream.opaque = (void *)0;
82
83
2.51k
  d_stream.next_in = compr;
84
2.51k
  d_stream.avail_in = 0;
85
2.51k
  d_stream.next_out = uncompr;
86
87
2.51k
  err = inflateInit(&d_stream);
88
2.51k
  CHECK_ERR(err, "inflateInit");
89
90
139M
  while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
91
139M
    d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
92
139M
    err = inflate(&d_stream, Z_NO_FLUSH);
93
139M
    if (err == Z_STREAM_END)
94
0
      break;
95
139M
    CHECK_ERR(err, "inflate");
96
139M
  }
97
98
2.51k
  err = inflateEnd(&d_stream);
99
2.51k
  CHECK_ERR(err, "inflateEnd");
100
101
2.51k
  if (memcmp(uncompr, data, dataLen)) {
102
0
    fprintf(stderr, "bad inflate\n");
103
0
    return 0;
104
0
  }
105
2.51k
  return 0;
106
2.51k
}
107
108
3.66k
int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) {
109
3.66k
  size_t comprLen = compressBound(size);
110
3.66k
  size_t uncomprLen = size;
111
3.66k
  uint8_t *compr, *uncompr;
112
113
  /* Discard inputs larger than 1Mb. */
114
3.66k
  static size_t kMaxSize = 1024 * 1024;
115
116
3.66k
  if (size < 1 || size > kMaxSize)
117
4
    return 0;
118
119
3.66k
  data = d;
120
3.66k
  dataLen = size;
121
3.66k
  compr = (uint8_t *)calloc(1, comprLen);
122
3.66k
  uncompr = (uint8_t *)calloc(1, uncomprLen);
123
3.66k
  if (compr == NULL || uncompr == NULL) {
124
0
    free(compr);
125
0
    free(uncompr);
126
0
    return 0;
127
0
  }
128
129
3.66k
  test_deflate(compr, comprLen);
130
3.66k
  test_inflate(compr, comprLen, uncompr, uncomprLen);
131
132
3.66k
  free(compr);
133
3.66k
  free(uncompr);
134
135
  /* This function must return 0. */
136
3.66k
  return 0;
137
3.66k
}