Coverage Report

Created: 2026-05-30 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brotli/c/fuzz/decode_fuzzer.c
Line
Count
Source
1
// Copyright 2015 The Chromium Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
#include <stddef.h>
6
#include <stdint.h>
7
#include <stdlib.h>
8
9
#include <brotli/decode.h>
10
11
// Entry point for LibFuzzer.
12
4.70k
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
13
4.70k
  size_t addend = 0;
14
4.70k
  if (size > 0)
15
4.70k
    addend = data[size - 1] & 7;
16
4.70k
  const uint8_t* next_in = data;
17
18
4.70k
  const int kBufferSize = 1024;
19
4.70k
  uint8_t* buffer = (uint8_t*) malloc(kBufferSize);
20
4.70k
  if (!buffer) {
21
    // OOM is out-of-scope here.
22
0
    return 0;
23
0
  }
24
  /* The biggest "magic number" in brotli is 16MiB - 16, so no need to check
25
     the cases with much longer output. */
26
4.70k
  const size_t total_out_limit = (addend == 0) ? (1 << 26) : (1 << 24);
27
4.70k
  size_t total_out = 0;
28
29
4.70k
  BrotliDecoderState* state = BrotliDecoderCreateInstance(0, 0, 0);
30
4.70k
  if (!state) {
31
    // OOM is out-of-scope here.
32
0
    free(buffer);
33
0
    return 0;
34
0
  }
35
36
4.70k
  if (addend == 0)
37
2.11k
    addend = size;
38
  /* Test both fast (addend == size) and slow (addend <= 7) decoding paths. */
39
955k
  for (size_t i = 0; i < size;) {
40
952k
    size_t next_i = i + addend;
41
952k
    if (next_i > size)
42
1.05k
      next_i = size;
43
952k
    size_t avail_in = next_i - i;
44
952k
    i = next_i;
45
952k
    BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
46
5.27M
    while (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
47
4.32M
      size_t avail_out = kBufferSize;
48
4.32M
      uint8_t* next_out = buffer;
49
4.32M
      result = BrotliDecoderDecompressStream(
50
4.32M
          state, &avail_in, &next_in, &avail_out, &next_out, &total_out);
51
4.32M
      if (total_out > total_out_limit)
52
12
        break;
53
4.32M
    }
54
952k
    if (total_out > total_out_limit)
55
12
      break;
56
952k
    if (result != BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT)
57
1.55k
      break;
58
952k
  }
59
60
4.70k
  BrotliDecoderDestroyInstance(state);
61
4.70k
  free(buffer);
62
4.70k
  return 0;
63
4.70k
}