]>
Commit | Line | Data |
---|---|---|
c25aa7cd PP |
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 "buffer.h" | |
15 | #include "common.h" | |
16 | #include "futils.h" | |
17 | #include "hash.h" | |
18 | #include "commit_graph.h" | |
19 | ||
20 | int LLVMFuzzerInitialize(int *argc, char ***argv) | |
21 | { | |
22 | GIT_UNUSED(argc); | |
23 | GIT_UNUSED(argv); | |
24 | ||
25 | if (git_libgit2_init() < 0) { | |
26 | fprintf(stderr, "Failed to initialize libgit2\n"); | |
27 | abort(); | |
28 | } | |
29 | return 0; | |
30 | } | |
31 | ||
32 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) | |
33 | { | |
34 | git_commit_graph_file file = {{0}}; | |
35 | git_commit_graph_entry e; | |
36 | git_buf commit_graph_buf = GIT_BUF_INIT; | |
37 | git_oid oid = {{0}}; | |
38 | bool append_hash = false; | |
39 | ||
40 | if (size < 4) | |
41 | return 0; | |
42 | ||
43 | /* | |
44 | * If the first byte in the stream has the high bit set, append the | |
45 | * SHA1 hash so that the file is somewhat valid. | |
46 | */ | |
47 | append_hash = *data & 0x80; | |
48 | /* Keep a 4-byte alignment to avoid unaligned accesses. */ | |
49 | data += 4; | |
50 | size -= 4; | |
51 | ||
52 | if (append_hash) { | |
53 | if (git_buf_init(&commit_graph_buf, size + sizeof(oid)) < 0) | |
54 | goto cleanup; | |
55 | if (git_hash_buf(&oid, data, size) < 0) { | |
56 | fprintf(stderr, "Failed to compute the SHA1 hash\n"); | |
57 | abort(); | |
58 | } | |
59 | memcpy(commit_graph_buf.ptr, data, size); | |
60 | memcpy(commit_graph_buf.ptr + size, &oid, sizeof(oid)); | |
61 | } else { | |
62 | git_buf_attach_notowned(&commit_graph_buf, (char *)data, size); | |
63 | } | |
64 | ||
65 | if (git_commit_graph_file_parse( | |
66 | &file, | |
67 | (const unsigned char *)git_buf_cstr(&commit_graph_buf), | |
68 | git_buf_len(&commit_graph_buf)) | |
69 | < 0) | |
70 | goto cleanup; | |
71 | ||
72 | /* Search for any oid, just to exercise that codepath. */ | |
73 | if (git_commit_graph_entry_find(&e, &file, &oid, GIT_OID_HEXSZ) < 0) | |
74 | goto cleanup; | |
75 | ||
76 | cleanup: | |
77 | git_commit_graph_file_close(&file); | |
78 | git_buf_dispose(&commit_graph_buf); | |
79 | return 0; | |
80 | } |