1616#include "git2.h"
1717#include "git2/sys/mempack.h"
1818#include "common.h"
19+ #include "buffer.h"
1920
2021static git_odb * odb = NULL ;
2122static git_odb_backend * mempack = NULL ;
@@ -54,12 +55,11 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
5455
5556int LLVMFuzzerTestOneInput (const uint8_t * data , size_t size )
5657{
57- git_indexer * indexer = NULL ;
5858 git_indexer_progress stats = {0 , 0 };
59+ git_indexer * indexer = NULL ;
60+ git_buf path = GIT_BUF_INIT ;
61+ git_oid oid ;
5962 bool append_hash = false;
60- git_oid id ;
61- char hash [GIT_OID_HEXSZ + 1 ] = {0 };
62- char path [GIT_PATH_MAX ];
6363
6464 if (size == 0 )
6565 return 0 ;
@@ -70,7 +70,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
7070 }
7171 git_mempack_reset (mempack );
7272
73- if (git_odb_write (& id , odb , base_obj , base_obj_len , GIT_OBJECT_BLOB ) < 0 ) {
73+ if (git_odb_write (& oid , odb , base_obj , base_obj_len , GIT_OBJECT_BLOB ) < 0 ) {
7474 fprintf (stderr , "Failed to add an object to the odb\n" );
7575 abort ();
7676 }
@@ -92,7 +92,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
9292 if (git_indexer_append (indexer , data , size , & stats ) < 0 )
9393 goto cleanup ;
9494 if (append_hash ) {
95- git_oid oid ;
9695 if (git_odb_hash (& oid , data , size , GIT_OBJECT_BLOB ) < 0 ) {
9796 fprintf (stderr , "Failed to compute the SHA1 hash\n" );
9897 abort ();
@@ -104,19 +103,19 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
104103 if (git_indexer_commit (indexer , & stats ) < 0 )
105104 goto cleanup ;
106105
107- /*
108- * We made it! We managed to produce a valid packfile.
109- * Let's clean it up.
110- */
111- git_oid_fmt (hash , git_indexer_hash (indexer ));
112- printf ("Generated packfile %s\n" , hash );
113- snprintf (path , sizeof (path ), "pack-%s.idx" , hash );
114- p_unlink (path );
115- snprintf (path , sizeof (path ), "pack-%s.pack" , hash );
116- p_unlink (path );
106+ if (git_buf_printf (& path , "pack-%s.idx" , git_oid_tostr_s (git_indexer_hash (indexer ))) < 0 )
107+ goto cleanup ;
108+ p_unlink (git_buf_cstr (& path ));
109+
110+ git_buf_clear (& path );
111+
112+ if (git_buf_printf (& path , "pack-%s.pack" , git_oid_tostr_s (git_indexer_hash (indexer ))) < 0 )
113+ goto cleanup ;
114+ p_unlink (git_buf_cstr (& path ));
117115
118116cleanup :
119117 git_mempack_reset (mempack );
120118 git_indexer_free (indexer );
119+ git_buf_dispose (& path );
121120 return 0 ;
122121}
0 commit comments