]>
Commit | Line | Data |
---|---|---|
22a2d3d5 UG |
1 | /* |
2 | * libgit2 multi-pack-index 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 "midx.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_midx_file idx = {{0}}; | |
35 | git_midx_entry e; | |
36 | git_buf midx_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 packfile 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(&midx_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(midx_buf.ptr, data, size); | |
60 | memcpy(midx_buf.ptr + size, &oid, sizeof(oid)); | |
61 | } else { | |
62 | git_buf_attach_notowned(&midx_buf, (char *)data, size); | |
63 | } | |
64 | ||
65 | if (git_midx_parse(&idx, (const unsigned char *)git_buf_cstr(&midx_buf), git_buf_len(&midx_buf)) < 0) | |
66 | goto cleanup; | |
67 | ||
68 | /* Search for any oid, just to exercise that codepath. */ | |
69 | if (git_midx_entry_find(&e, &idx, &oid, GIT_OID_HEXSZ) < 0) | |
70 | goto cleanup; | |
71 | ||
72 | cleanup: | |
73 | git_midx_close(&idx); | |
74 | git_buf_dispose(&midx_buf); | |
75 | return 0; | |
76 | } |