]> git.proxmox.com Git - libgit2.git/blame - fuzzers/midx_fuzzer.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / fuzzers / midx_fuzzer.c
CommitLineData
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
22a2d3d5
UG
14#include "common.h"
15#include "futils.h"
16#include "hash.h"
17#include "midx.h"
18
e579e0f7
MB
19#include "standalone_driver.h"
20
22a2d3d5
UG
21int LLVMFuzzerInitialize(int *argc, char ***argv)
22{
23 GIT_UNUSED(argc);
24 GIT_UNUSED(argv);
25
26 if (git_libgit2_init() < 0) {
27 fprintf(stderr, "Failed to initialize libgit2\n");
28 abort();
29 }
30 return 0;
31}
32
33int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
34{
35 git_midx_file idx = {{0}};
36 git_midx_entry e;
e579e0f7
MB
37 git_str midx_buf = GIT_STR_INIT;
38 unsigned char hash[GIT_HASH_SHA1_SIZE];
22a2d3d5
UG
39 git_oid oid = {{0}};
40 bool append_hash = false;
41
42 if (size < 4)
43 return 0;
44
45 /*
46 * If the first byte in the stream has the high bit set, append the
47 * SHA1 hash so that the packfile is somewhat valid.
48 */
49 append_hash = *data & 0x80;
50 /* Keep a 4-byte alignment to avoid unaligned accesses. */
51 data += 4;
52 size -= 4;
53
54 if (append_hash) {
e579e0f7 55 if (git_str_init(&midx_buf, size + GIT_HASH_SHA1_SIZE) < 0)
22a2d3d5 56 goto cleanup;
e579e0f7 57 if (git_hash_buf(hash, data, size, GIT_HASH_ALGORITHM_SHA1) < 0) {
22a2d3d5
UG
58 fprintf(stderr, "Failed to compute the SHA1 hash\n");
59 abort();
60 }
61 memcpy(midx_buf.ptr, data, size);
e579e0f7
MB
62 memcpy(midx_buf.ptr + size, hash, GIT_HASH_SHA1_SIZE);
63
64 memcpy(oid.id, hash, GIT_OID_RAWSZ);
22a2d3d5 65 } else {
e579e0f7 66 git_str_attach_notowned(&midx_buf, (char *)data, size);
22a2d3d5
UG
67 }
68
e579e0f7 69 if (git_midx_parse(&idx, (const unsigned char *)git_str_cstr(&midx_buf), git_str_len(&midx_buf)) < 0)
22a2d3d5
UG
70 goto cleanup;
71
72 /* Search for any oid, just to exercise that codepath. */
73 if (git_midx_entry_find(&e, &idx, &oid, GIT_OID_HEXSZ) < 0)
74 goto cleanup;
75
76cleanup:
77 git_midx_close(&idx);
e579e0f7 78 git_str_dispose(&midx_buf);
22a2d3d5
UG
79 return 0;
80}