]> git.proxmox.com Git - libgit2.git/blobdiff - fuzzers/midx_fuzzer.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / fuzzers / midx_fuzzer.c
index e67873faa536450ff3595e30b8d4b2072c880773..4c3124e47823d7ff3eef2d846014e4a810018294 100644 (file)
 
 #include "git2.h"
 
-#include "buffer.h"
 #include "common.h"
 #include "futils.h"
 #include "hash.h"
 #include "midx.h"
 
+#include "standalone_driver.h"
+
 int LLVMFuzzerInitialize(int *argc, char ***argv)
 {
        GIT_UNUSED(argc);
@@ -33,7 +34,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 {
        git_midx_file idx = {{0}};
        git_midx_entry e;
-       git_buf midx_buf = GIT_BUF_INIT;
+       git_str midx_buf = GIT_STR_INIT;
+       unsigned char hash[GIT_HASH_SHA1_SIZE];
        git_oid oid = {{0}};
        bool append_hash = false;
 
@@ -50,19 +52,21 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
        size -= 4;
 
        if (append_hash) {
-               if (git_buf_init(&midx_buf, size + sizeof(oid)) < 0)
+               if (git_str_init(&midx_buf, size + GIT_HASH_SHA1_SIZE) < 0)
                        goto cleanup;
-               if (git_hash_buf(&oid, data, size) < 0) {
+               if (git_hash_buf(hash, data, size, GIT_HASH_ALGORITHM_SHA1) < 0) {
                        fprintf(stderr, "Failed to compute the SHA1 hash\n");
                        abort();
                }
                memcpy(midx_buf.ptr, data, size);
-               memcpy(midx_buf.ptr + size, &oid, sizeof(oid));
+               memcpy(midx_buf.ptr + size, hash, GIT_HASH_SHA1_SIZE);
+
+               memcpy(oid.id, hash, GIT_OID_RAWSZ);
        } else {
-               git_buf_attach_notowned(&midx_buf, (char *)data, size);
+               git_str_attach_notowned(&midx_buf, (char *)data, size);
        }
 
-       if (git_midx_parse(&idx, (const unsigned char *)git_buf_cstr(&midx_buf), git_buf_len(&midx_buf)) < 0)
+       if (git_midx_parse(&idx, (const unsigned char *)git_str_cstr(&midx_buf), git_str_len(&midx_buf)) < 0)
                goto cleanup;
 
        /* Search for any oid, just to exercise that codepath. */
@@ -71,6 +75,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 
 cleanup:
        git_midx_close(&idx);
-       git_buf_dispose(&midx_buf);
+       git_str_dispose(&midx_buf);
        return 0;
 }