Merge pull request #4842 from nelhage/fuzz-config-memory config: Port config_file_fuzzer to the new in-memory backend.
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
diff --git a/fuzzers/config_file_fuzzer.c b/fuzzers/config_file_fuzzer.c
index 30a47bf..fa52642 100644
--- a/fuzzers/config_file_fuzzer.c
+++ b/fuzzers/config_file_fuzzer.c
@@ -8,6 +8,7 @@
*/
#include <git2.h>
+#include "config_backend.h"
#include <stdlib.h>
#include <stdio.h>
@@ -25,9 +26,6 @@ int foreach_cb(const git_config_entry *entry, void *payload)
return 0;
}
-static char path[] = "/tmp/git.XXXXXX";
-static int fd = -1;
-
int LLVMFuzzerInitialize(int *argc, char ***argv)
{
UNUSED(argc);
@@ -35,10 +33,6 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
if (git_libgit2_init() < 0)
abort();
- fd = mkstemp(path);
- if (fd < 0) {
- abort();
- }
return 0;
}
@@ -46,30 +40,26 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
git_config *cfg = NULL;
+ git_config_backend *backend = NULL;
int err = 0;
- size_t total = 0;
- if (ftruncate(fd, 0) !=0 ) {
- abort();
- }
- if (lseek(fd, 0, SEEK_SET) != 0) {
- abort();
+ if ((err = git_config_new(&cfg)) != 0) {
+ goto out;
}
- while (total < size) {
- ssize_t written = write(fd, data, size);
- if (written < 0 && errno != EINTR)
- abort();
- if (written < 0)
- continue;
- total += written;
+ if ((err = git_config_backend_from_string(&backend, (const char*)data, size)) != 0) {
+ goto out;
}
-
- err = git_config_open_ondisk(&cfg, path);
- if (err == 0) {
- git_config_foreach(cfg, foreach_cb, NULL);
- git_config_free(cfg);
+ if ((err = git_config_add_backend(cfg, backend, 0, NULL, 0)) != 0) {
+ goto out;
}
+ /* Now owned by the config */
+ backend = NULL;
+
+ git_config_foreach(cfg, foreach_cb, NULL);
+ out:
+ git_config_backend_free(backend);
+ git_config_free(cfg);
return 0;
}
diff --git a/src/config_backend.h b/src/config_backend.h
index 2451f9a..6d678ae 100644
--- a/src/config_backend.h
+++ b/src/config_backend.h
@@ -30,8 +30,9 @@ extern int git_config_backend_from_file(git_config_backend **out, const char *pa
*
* @param out the new backend
* @param cfg the configuration that is to be parsed
+ * @param len the length of the string pointed to by `cfg`
*/
-extern int git_config_backend_from_string(git_config_backend **out, const char *cfg);
+extern int git_config_backend_from_string(git_config_backend **out, const char *cfg, size_t len);
GIT_INLINE(int) git_config_backend_open(git_config_backend *cfg, unsigned int level, const git_repository *repo)
{
diff --git a/src/config_mem.c b/src/config_mem.c
index fbb6373..18e405a 100644
--- a/src/config_mem.c
+++ b/src/config_mem.c
@@ -186,7 +186,7 @@ static void config_memory_free(git_config_backend *_backend)
git__free(backend);
}
-int git_config_backend_from_string(git_config_backend **out, const char *cfg)
+int git_config_backend_from_string(git_config_backend **out, const char *cfg, size_t len)
{
config_memory_backend *backend;
@@ -198,7 +198,7 @@ int git_config_backend_from_string(git_config_backend **out, const char *cfg)
return -1;
}
- if (git_buf_sets(&backend->cfg, cfg) < 0) {
+ if (git_buf_set(&backend->cfg, cfg, len) < 0) {
git_config_entries_free(backend->entries);
git__free(backend);
return -1;
diff --git a/tests/config/memory.c b/tests/config/memory.c
index aed221c..ae66189 100644
--- a/tests/config/memory.c
+++ b/tests/config/memory.c
@@ -61,7 +61,7 @@ static void assert_config_contains_all(git_config_backend *backend,
static void setup_backend(const char *cfg)
{
- cl_git_pass(git_config_backend_from_string(&backend, cfg));
+ cl_git_pass(git_config_backend_from_string(&backend, cfg, strlen(cfg)));
cl_git_pass(git_config_backend_open(backend, 0, NULL));
}
@@ -85,9 +85,10 @@ void test_config_memory__simple(void)
void test_config_memory__malformed_fails_to_open(void)
{
- cl_git_pass(git_config_backend_from_string(&backend,
+ const char *cfg =
"[general\n"
- "foo=bar\n"));
+ "foo=bar\n";
+ cl_git_pass(git_config_backend_from_string(&backend, cfg, strlen(cfg)));
cl_git_fail(git_config_backend_open(backend, 0, NULL));
}