Coverage Report

Created: 2025-07-11 07:02

/src/bzip2_filename.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
# Copyright 2022 Google LLC
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#      http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
#
16
###############################################################################
17
*/
18
19
#include "bzlib.h"
20
#include <stdint.h>
21
#include <stdlib.h>
22
#include <assert.h>
23
#include <string.h>
24
#include <stddef.h>
25
#include <unistd.h>
26
#include <stdio.h>
27
#include <stdbool.h>
28
29
3.64k
static void fuzzer_write_data(FILE *file, const uint8_t *data, size_t size) {
30
3.64k
  int    bzerr         = 0;
31
3.64k
  int    blockSize100k = 9;
32
3.64k
  int    verbosity     = 0;
33
3.64k
  int    workFactor    = 30;
34
3.64k
  uint   nbytes_in_lo32, nbytes_in_hi32;
35
3.64k
  uint   nbytes_out_lo32, nbytes_out_hi32;
36
37
3.64k
  BZFILE* bzf = BZ2_bzWriteOpen ( &bzerr, file,
38
3.64k
                           blockSize100k, verbosity, workFactor );
39
40
3.64k
  BZ2_bzWrite (&bzerr, bzf, (void*)data, size);
41
42
3.64k
  BZ2_bzWriteClose64 ( &bzerr, bzf, 0,
43
3.64k
                        &nbytes_in_lo32, &nbytes_in_hi32,
44
3.64k
                        &nbytes_out_lo32, &nbytes_out_hi32 );
45
3.64k
  return;
46
3.64k
}
47
48
3.64k
static void fuzzer_read_data(char *filename) {
49
3.64k
  int    bzerr          = 0;
50
3.64k
  char   obuf[BZ_MAX_UNUSED];
51
 
52
3.64k
  BZFILE* bzf2 = BZ2_bzopen(filename,"rb");
53
54
79.9k
  while (bzerr == BZ_OK) {
55
76.3k
      BZ2_bzRead ( &bzerr, bzf2, obuf, BZ_MAX_UNUSED);
56
76.3k
  }
57
58
3.64k
  BZ2_bzReadClose ( &bzerr, bzf2);
59
3.64k
  return;
60
3.64k
}
61
62
int
63
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64
3.64k
{
65
3.64k
  char* filename = strdup("/tmp/generate_temporary_file.XXXXXX");
66
3.64k
  if (!filename) {
67
0
    perror("Failed to allocate file name buffer.");
68
0
    abort();
69
0
  }
70
3.64k
  const int file_descriptor = mkstemp(filename);
71
3.64k
  if (file_descriptor < 0) {
72
0
    perror("Failed to make temporary file.");
73
0
    abort();
74
0
  }
75
3.64k
  FILE* file = fdopen(file_descriptor, "wb");
76
3.64k
  if (!file) {
77
0
    perror("Failed to open file descriptor.");
78
0
    close(file_descriptor);
79
0
    abort();
80
0
  }
81
82
3.64k
  fuzzer_write_data(file, data, size);
83
84
3.64k
  fuzzer_read_data(filename);
85
86
3.64k
  BZ2_bzlibVersion();
87
88
3.64k
  BZ2_bzflush(file);
89
3.64k
  fclose(file);
90
91
3.64k
  if (unlink(filename) != 0) {
92
0
    perror("WARNING: Failed to delete temporary file.");
93
0
  }
94
3.64k
  free(filename);
95
3.64k
  return 0;
96
3.64k
}