Coverage Report

Created: 2023-06-07 06:32

/src/libgit2/fuzzers/commit_graph_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * libgit2 commit-graph fuzzer target.
3
 *
4
 * Copyright (C) the libgit2 contributors. All rights reserved.
5
 *
6
 * This file is part of libgit2, distributed under the GNU GPL v2 with
7
 * a Linking Exception. For full terms see the included COPYING file.
8
 */
9
10
#include <stdio.h>
11
12
#include "git2.h"
13
14
#include "common.h"
15
#include "str.h"
16
#include "futils.h"
17
#include "hash.h"
18
#include "commit_graph.h"
19
20
#include "standalone_driver.h"
21
22
int LLVMFuzzerInitialize(int *argc, char ***argv)
23
4
{
24
4
  GIT_UNUSED(argc);
25
4
  GIT_UNUSED(argv);
26
27
4
  if (git_libgit2_init() < 0) {
28
0
    fprintf(stderr, "Failed to initialize libgit2\n");
29
0
    abort();
30
0
  }
31
4
  return 0;
32
4
}
33
34
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
35
3.61k
{
36
3.61k
  git_commit_graph_file file = {{0}};
37
3.61k
  git_commit_graph_entry e;
38
3.61k
  git_str commit_graph_buf = GIT_STR_INIT;
39
3.61k
  unsigned char hash[GIT_HASH_SHA1_SIZE];
40
3.61k
  git_oid oid = GIT_OID_NONE;
41
3.61k
  bool append_hash = false;
42
43
3.61k
  if (size < 4)
44
4
    return 0;
45
46
  /*
47
   * If the first byte in the stream has the high bit set, append the
48
   * SHA1 hash so that the file is somewhat valid.
49
   */
50
3.60k
  append_hash = *data & 0x80;
51
  /* Keep a 4-byte alignment to avoid unaligned accesses. */
52
3.60k
  data += 4;
53
3.60k
  size -= 4;
54
55
3.60k
  if (append_hash) {
56
1.87k
    if (git_str_init(&commit_graph_buf, size + GIT_HASH_SHA1_SIZE) < 0)
57
0
      goto cleanup;
58
1.87k
    if (git_hash_buf(hash, data, size, GIT_HASH_ALGORITHM_SHA1) < 0) {
59
0
      fprintf(stderr, "Failed to compute the SHA1 hash\n");
60
0
      abort();
61
0
    }
62
1.87k
    memcpy(commit_graph_buf.ptr, data, size);
63
1.87k
    memcpy(commit_graph_buf.ptr + size, hash, GIT_HASH_SHA1_SIZE);
64
65
1.87k
    memcpy(oid.id, hash, GIT_OID_SHA1_SIZE);
66
1.87k
  } else {
67
1.72k
    git_str_attach_notowned(&commit_graph_buf, (char *)data, size);
68
1.72k
  }
69
70
3.60k
  if (git_commit_graph_file_parse(
71
3.60k
          &file,
72
3.60k
          (const unsigned char *)git_str_cstr(&commit_graph_buf),
73
3.60k
          git_str_len(&commit_graph_buf))
74
3.60k
      < 0)
75
3.60k
    goto cleanup;
76
77
  /* Search for any oid, just to exercise that codepath. */
78
0
  if (git_commit_graph_entry_find(&e, &file, &oid, GIT_OID_SHA1_HEXSIZE) < 0)
79
0
    goto cleanup;
80
81
3.60k
cleanup:
82
3.60k
  git_commit_graph_file_close(&file);
83
3.60k
  git_str_dispose(&commit_graph_buf);
84
3.60k
  return 0;
85
0
}