fuzzers: use `git_buf_printf` instead of `snprintf` The `snprintf` function does not exist on Win32, it only has `_snprintf_s` available. Let's just avoid any cross-platform hassle and use our own `git_buf` functionality instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
diff --git a/fuzzers/packfile_fuzzer.c b/fuzzers/packfile_fuzzer.c
index b42c6f5..672f557 100644
--- a/fuzzers/packfile_fuzzer.c
+++ b/fuzzers/packfile_fuzzer.c
@@ -16,6 +16,7 @@
#include "git2.h"
#include "git2/sys/mempack.h"
#include "common.h"
+#include "buffer.h"
static git_odb *odb = NULL;
static git_odb_backend *mempack = NULL;
@@ -54,12 +55,11 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
- git_indexer *indexer = NULL;
git_indexer_progress stats = {0, 0};
+ git_indexer *indexer = NULL;
+ git_buf path = GIT_BUF_INIT;
+ git_oid oid;
bool append_hash = false;
- git_oid id;
- char hash[GIT_OID_HEXSZ + 1] = {0};
- char path[GIT_PATH_MAX];
if (size == 0)
return 0;
@@ -70,7 +70,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
}
git_mempack_reset(mempack);
- if (git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB) < 0) {
+ if (git_odb_write(&oid, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB) < 0) {
fprintf(stderr, "Failed to add an object to the odb\n");
abort();
}
@@ -92,7 +92,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (git_indexer_append(indexer, data, size, &stats) < 0)
goto cleanup;
if (append_hash) {
- git_oid oid;
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
fprintf(stderr, "Failed to compute the SHA1 hash\n");
abort();
@@ -104,19 +103,19 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (git_indexer_commit(indexer, &stats) < 0)
goto cleanup;
- /*
- * We made it! We managed to produce a valid packfile.
- * Let's clean it up.
- */
- git_oid_fmt(hash, git_indexer_hash(indexer));
- printf("Generated packfile %s\n", hash);
- snprintf(path, sizeof(path), "pack-%s.idx", hash);
- p_unlink(path);
- snprintf(path, sizeof(path), "pack-%s.pack", hash);
- p_unlink(path);
+ if (git_buf_printf(&path, "pack-%s.idx", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
+ goto cleanup;
+ p_unlink(git_buf_cstr(&path));
+
+ git_buf_clear(&path);
+
+ if (git_buf_printf(&path, "pack-%s.pack", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
+ goto cleanup;
+ p_unlink(git_buf_cstr(&path));
cleanup:
git_mempack_reset(mempack);
git_indexer_free(indexer);
+ git_buf_dispose(&path);
return 0;
}