Skip to content

Commit 814e7ac

Browse files
authored
Merge pull request libgit2#4842 from nelhage/fuzz-config-memory
config: Port config_file_fuzzer to the new in-memory backend.
2 parents 838a2f2 + 463c21e commit 814e7ac

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

fuzzers/config_file_fuzzer.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
#include <git2.h>
11+
#include "config_backend.h"
1112

1213
#include <stdlib.h>
1314
#include <stdio.h>
@@ -25,51 +26,40 @@ int foreach_cb(const git_config_entry *entry, void *payload)
2526
return 0;
2627
}
2728

28-
static char path[] = "/tmp/git.XXXXXX";
29-
static int fd = -1;
30-
3129
int LLVMFuzzerInitialize(int *argc, char ***argv)
3230
{
3331
UNUSED(argc);
3432
UNUSED(argv);
3533

3634
if (git_libgit2_init() < 0)
3735
abort();
38-
fd = mkstemp(path);
39-
if (fd < 0) {
40-
abort();
41-
}
4236

4337
return 0;
4438
}
4539

4640
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
4741
{
4842
git_config *cfg = NULL;
43+
git_config_backend *backend = NULL;
4944
int err = 0;
50-
size_t total = 0;
5145

52-
if (ftruncate(fd, 0) !=0 ) {
53-
abort();
54-
}
55-
if (lseek(fd, 0, SEEK_SET) != 0) {
56-
abort();
46+
if ((err = git_config_new(&cfg)) != 0) {
47+
goto out;
5748
}
5849

59-
while (total < size) {
60-
ssize_t written = write(fd, data, size);
61-
if (written < 0 && errno != EINTR)
62-
abort();
63-
if (written < 0)
64-
continue;
65-
total += written;
50+
if ((err = git_config_backend_from_string(&backend, (const char*)data, size)) != 0) {
51+
goto out;
6652
}
67-
68-
err = git_config_open_ondisk(&cfg, path);
69-
if (err == 0) {
70-
git_config_foreach(cfg, foreach_cb, NULL);
71-
git_config_free(cfg);
53+
if ((err = git_config_add_backend(cfg, backend, 0, NULL, 0)) != 0) {
54+
goto out;
7255
}
56+
/* Now owned by the config */
57+
backend = NULL;
58+
59+
git_config_foreach(cfg, foreach_cb, NULL);
60+
out:
61+
git_config_backend_free(backend);
62+
git_config_free(cfg);
7363

7464
return 0;
7565
}

src/config_backend.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ extern int git_config_backend_from_file(git_config_backend **out, const char *pa
3030
*
3131
* @param out the new backend
3232
* @param cfg the configuration that is to be parsed
33+
* @param len the length of the string pointed to by `cfg`
3334
*/
34-
extern int git_config_backend_from_string(git_config_backend **out, const char *cfg);
35+
extern int git_config_backend_from_string(git_config_backend **out, const char *cfg, size_t len);
3536

3637
GIT_INLINE(int) git_config_backend_open(git_config_backend *cfg, unsigned int level, const git_repository *repo)
3738
{

src/config_mem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static void config_memory_free(git_config_backend *_backend)
186186
git__free(backend);
187187
}
188188

189-
int git_config_backend_from_string(git_config_backend **out, const char *cfg)
189+
int git_config_backend_from_string(git_config_backend **out, const char *cfg, size_t len)
190190
{
191191
config_memory_backend *backend;
192192

@@ -198,7 +198,7 @@ int git_config_backend_from_string(git_config_backend **out, const char *cfg)
198198
return -1;
199199
}
200200

201-
if (git_buf_sets(&backend->cfg, cfg) < 0) {
201+
if (git_buf_set(&backend->cfg, cfg, len) < 0) {
202202
git_config_entries_free(backend->entries);
203203
git__free(backend);
204204
return -1;

tests/config/memory.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static void assert_config_contains_all(git_config_backend *backend,
6161

6262
static void setup_backend(const char *cfg)
6363
{
64-
cl_git_pass(git_config_backend_from_string(&backend, cfg));
64+
cl_git_pass(git_config_backend_from_string(&backend, cfg, strlen(cfg)));
6565
cl_git_pass(git_config_backend_open(backend, 0, NULL));
6666
}
6767

@@ -85,9 +85,10 @@ void test_config_memory__simple(void)
8585

8686
void test_config_memory__malformed_fails_to_open(void)
8787
{
88-
cl_git_pass(git_config_backend_from_string(&backend,
88+
const char *cfg =
8989
"[general\n"
90-
"foo=bar\n"));
90+
"foo=bar\n";
91+
cl_git_pass(git_config_backend_from_string(&backend, cfg, strlen(cfg)));
9192
cl_git_fail(git_config_backend_open(backend, 0, NULL));
9293
}
9394

0 commit comments

Comments
 (0)