Coverage Report

Created: 2023-03-26 06:59

/src/lzma-fuzz/7z_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/**
2
 *
3
 * @copyright Copyright (c) 2019 Joachim Bauch <mail@joachim-bauch.de>
4
 *
5
 * @license GNU GPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
#include <stdint.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "7zCrc.h"
27
#include "7z.h"
28
29
#include "common-alloc.h"
30
#include "common-buffer.h"
31
32
// Limit maximum size to avoid running into timeouts with too large data.
33
static const size_t kMaxInputSize = 100 * 1024;
34
35
5.05k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
36
5.05k
  if (size > kMaxInputSize) {
37
3
    return 0;
38
3
  }
39
40
5.05k
  CSzArEx db;
41
5.05k
  SRes res;
42
5.05k
  UInt16 *temp = nullptr;
43
5.05k
  size_t tempSize = 0;
44
5.05k
  UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */
45
5.05k
  Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */
46
5.05k
  size_t outBufferSize = 0;  /* it can have any value before first call (if outBuffer = 0) */
47
48
5.05k
  CrcGenerateTable();
49
5.05k
  SzArEx_Init(&db);
50
51
5.05k
  InputLookBuffer buffer(data, size);
52
5.05k
  res = SzArEx_Open(&db, buffer.stream(), &CommonAlloc, &CommonAlloc);
53
5.05k
  if (res != SZ_OK) {
54
4.25k
    goto exit;
55
4.25k
  }
56
57
3.21k
  for (UInt32 i = 0; i < db.NumFiles; i++) {
58
2.40k
    size_t offset = 0;
59
2.40k
    size_t outSizeProcessed = 0;
60
61
2.40k
    int isDir = SzArEx_IsDir(&db, i);
62
2.40k
    size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL);
63
2.40k
    if (len > tempSize) {
64
1.61k
      free(temp);
65
1.61k
      tempSize = len;
66
1.61k
      temp = (UInt16 *)malloc(tempSize * sizeof(temp[0]));
67
1.61k
      if (!temp) {
68
0
        break;
69
0
      }
70
1.61k
    }
71
72
2.40k
    SzArEx_GetFileNameUtf16(&db, i, temp);
73
2.40k
    SzArEx_GetFileSize(&db, i);
74
2.40k
    if (isDir) {
75
802
      continue;
76
802
    }
77
78
1.60k
    SzArEx_Extract(&db, buffer.stream(), i, &blockIndex, &outBuffer,
79
1.60k
        &outBufferSize, &offset, &outSizeProcessed, &CommonAlloc, &CommonAlloc);
80
1.60k
  }
81
804
  ISzAlloc_Free(&CommonAlloc, outBuffer);
82
83
5.05k
exit:
84
5.05k
  SzArEx_Free(&db, &CommonAlloc);
85
5.05k
  free(temp);
86
5.05k
  return 0;
87
804
}