Skip to content

Commit f63ac61

Browse files
committed
Merge branch 'ab/test-tool-leakfix'
Plug various memory leaks in test-tool commands. * ab/test-tool-leakfix: test-tool delta: fix a memory leak test-tool ref-store: fix a memory leak test-tool bloom: fix memory leaks test-tool json-writer: fix memory leaks test-tool regex: call regfree(), fix memory leaks test-tool urlmatch-normalization: fix a memory leak test-tool {dump,scrap}-cache-tree: fix memory leaks test-tool path-utils: fix a memory leak test-tool test-hash: fix a memory leak
2 parents 44357f6 + f40a693 commit f63ac61

21 files changed

+77
-24
lines changed

t/helper/test-bloom.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static void add_string_to_filter(const char *data, struct bloom_filter *filter)
1616
}
1717
printf("\n");
1818
add_key_to_filter(&key, filter, &settings);
19+
clear_bloom_key(&key);
1920
}
2021

2122
static void print_bloom_filter(struct bloom_filter *filter) {
@@ -80,6 +81,7 @@ int cmd__bloom(int argc, const char **argv)
8081
}
8182

8283
print_bloom_filter(&filter);
84+
free(filter.data);
8385
}
8486

8587
if (!strcmp(argv[1], "get_filter_for_commit")) {

t/helper/test-delta.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ int cmd__delta(int argc, const char **argv)
2020
{
2121
int fd;
2222
struct stat st;
23-
void *from_buf, *data_buf, *out_buf;
23+
void *from_buf = NULL, *data_buf = NULL, *out_buf = NULL;
2424
unsigned long from_size, data_size, out_size;
25+
int ret = 1;
2526

2627
if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) {
2728
fprintf(stderr, "usage: %s\n", usage_str);
@@ -38,21 +39,21 @@ int cmd__delta(int argc, const char **argv)
3839
if (read_in_full(fd, from_buf, from_size) < 0) {
3940
perror(argv[2]);
4041
close(fd);
41-
return 1;
42+
goto cleanup;
4243
}
4344
close(fd);
4445

4546
fd = open(argv[3], O_RDONLY);
4647
if (fd < 0 || fstat(fd, &st)) {
4748
perror(argv[3]);
48-
return 1;
49+
goto cleanup;
4950
}
5051
data_size = st.st_size;
5152
data_buf = xmalloc(data_size);
5253
if (read_in_full(fd, data_buf, data_size) < 0) {
5354
perror(argv[3]);
5455
close(fd);
55-
return 1;
56+
goto cleanup;
5657
}
5758
close(fd);
5859

@@ -66,14 +67,20 @@ int cmd__delta(int argc, const char **argv)
6667
&out_size);
6768
if (!out_buf) {
6869
fprintf(stderr, "delta operation failed (returned NULL)\n");
69-
return 1;
70+
goto cleanup;
7071
}
7172

7273
fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
7374
if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) {
7475
perror(argv[4]);
75-
return 1;
76+
goto cleanup;
7677
}
7778

78-
return 0;
79+
ret = 0;
80+
cleanup:
81+
free(from_buf);
82+
free(data_buf);
83+
free(out_buf);
84+
85+
return ret;
7986
}

t/helper/test-dump-cache-tree.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,16 @@ int cmd__dump_cache_tree(int ac, const char **av)
5959
{
6060
struct index_state istate;
6161
struct cache_tree *another = cache_tree();
62+
int ret;
63+
6264
setup_git_directory();
6365
if (read_cache() < 0)
6466
die("unable to read index file");
6567
istate = the_index;
6668
istate.cache_tree = another;
6769
cache_tree_update(&istate, WRITE_TREE_DRY_RUN);
68-
return dump_cache_tree(active_cache_tree, another, "");
70+
ret = dump_cache_tree(active_cache_tree, another, "");
71+
cache_tree_free(&another);
72+
73+
return ret;
6974
}

t/helper/test-hash.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ int cmd_hash_impl(int ac, const char **av, int algo)
5454
fwrite(hash, 1, algop->rawsz, stdout);
5555
else
5656
puts(hash_to_hex_algop(hash, algop));
57+
free(buffer);
5758
return 0;
5859
}

t/helper/test-json-writer.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,18 @@ static struct json_writer nest1 = JSON_WRITER_INIT;
181181

182182
static void make_nest1(int pretty)
183183
{
184+
make_obj1(0);
185+
make_arr1(0);
186+
184187
jw_object_begin(&nest1, pretty);
185188
{
186189
jw_object_sub_jw(&nest1, "obj1", &obj1);
187190
jw_object_sub_jw(&nest1, "arr1", &arr1);
188191
}
189192
jw_end(&nest1);
193+
194+
jw_release(&obj1);
195+
jw_release(&arr1);
190196
}
191197

192198
static char *expect_inline1 =
@@ -313,6 +319,9 @@ static void make_mixed1(int pretty)
313319
jw_object_sub_jw(&mixed1, "arr1", &arr1);
314320
}
315321
jw_end(&mixed1);
322+
323+
jw_release(&obj1);
324+
jw_release(&arr1);
316325
}
317326

318327
static void cmp(const char *test, const struct json_writer *jw, const char *exp)
@@ -325,8 +334,8 @@ static void cmp(const char *test, const struct json_writer *jw, const char *exp)
325334
exit(1);
326335
}
327336

328-
#define t(v) do { make_##v(0); cmp(#v, &v, expect_##v); } while (0)
329-
#define p(v) do { make_##v(1); cmp(#v, &v, pretty_##v); } while (0)
337+
#define t(v) do { make_##v(0); cmp(#v, &v, expect_##v); jw_release(&v); } while (0)
338+
#define p(v) do { make_##v(1); cmp(#v, &v, pretty_##v); jw_release(&v); } while (0)
330339

331340
/*
332341
* Run some basic regression tests with some known patterns.
@@ -381,7 +390,6 @@ static int unit_tests(void)
381390

382391
/* mixed forms */
383392
t(mixed1);
384-
jw_init(&mixed1);
385393
p(mixed1);
386394

387395
return 0;
@@ -544,7 +552,7 @@ static int scripted(void)
544552

545553
printf("%s\n", jw.json.buf);
546554

547-
strbuf_release(&jw.json);
555+
jw_release(&jw);
548556
return 0;
549557
}
550558

t/helper/test-path-utils.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,8 @@ int cmd__path_utils(int argc, const char **argv)
296296
if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
297297
char *buf = xmallocz(strlen(argv[2]));
298298
int rv = normalize_path_copy(buf, argv[2]);
299-
if (rv)
300-
buf = "++failed++";
301-
puts(buf);
299+
puts(rv ? "++failed++" : buf);
300+
free(buf);
302301
return 0;
303302
}
304303

@@ -356,7 +355,10 @@ int cmd__path_utils(int argc, const char **argv)
356355
int nongit_ok;
357356
setup_git_directory_gently(&nongit_ok);
358357
while (argc > 3) {
359-
puts(prefix_path(prefix, prefix_len, argv[3]));
358+
char *pfx = prefix_path(prefix, prefix_len, argv[3]);
359+
360+
puts(pfx);
361+
free(pfx);
360362
argc--;
361363
argv++;
362364
}
@@ -366,6 +368,7 @@ int cmd__path_utils(int argc, const char **argv)
366368
if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
367369
char *prefix = strip_path_suffix(argv[2], argv[3]);
368370
printf("%s\n", prefix ? prefix : "(null)");
371+
free(prefix);
369372
return 0;
370373
}
371374

t/helper/test-ref-store.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static const char **get_store(const char **argv, struct ref_store **refs)
9696
die("no such worktree: %s", gitdir);
9797

9898
*refs = get_worktree_ref_store(*p);
99+
free_worktrees(worktrees);
99100
} else
100101
die("unknown backend %s", argv[0]);
101102

t/helper/test-regex.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static int test_regex_bug(void)
3434
if (m[0].rm_so == 3) /* matches '\n' when it should not */
3535
die("regex bug confirmed: re-build git with NO_REGEX=1");
3636

37+
regfree(&r);
3738
return 0;
3839
}
3940

@@ -94,18 +95,20 @@ int cmd__regex(int argc, const char **argv)
9495
die("failed regcomp() for pattern '%s' (%s)", pat, errbuf);
9596
}
9697
if (!str)
97-
return 0;
98+
goto cleanup;
9899

99100
ret = regexec(&r, str, 1, m, 0);
100101
if (ret) {
101102
if (silent || ret == REG_NOMATCH)
102-
return ret;
103+
goto cleanup;
103104

104105
regerror(ret, &r, errbuf, sizeof(errbuf));
105106
die("failed regexec() for subject '%s' (%s)", str, errbuf);
106107
}
107108

108-
return 0;
109+
cleanup:
110+
regfree(&r);
111+
return ret;
109112
usage:
110113
usage("\ttest-tool regex --bug\n"
111114
"\ttest-tool regex [--silent] <pattern>\n"

t/helper/test-scrap-cache-tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int cmd__scrap_cache_tree(int ac, const char **av)
1212
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
1313
if (read_cache() < 0)
1414
die("unable to read index file");
15+
cache_tree_free(&active_cache_tree);
1516
active_cache_tree = NULL;
1617
if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
1718
die("unable to write index file");

t/helper/test-urlmatch-normalization.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
int cmd__urlmatch_normalization(int argc, const char **argv)
66
{
77
const char usage[] = "test-tool urlmatch-normalization [-p | -l] <url1> | <url1> <url2>";
8-
char *url1, *url2;
8+
char *url1 = NULL, *url2 = NULL;
99
int opt_p = 0, opt_l = 0;
10+
int ret = 0;
1011

1112
/*
1213
* For one url, succeed if url_normalize succeeds on it, fail otherwise.
@@ -39,13 +40,17 @@ int cmd__urlmatch_normalization(int argc, const char **argv)
3940
printf("%s\n", url1);
4041
if (opt_l)
4142
printf("%u\n", (unsigned)info.url_len);
42-
return 0;
43+
goto cleanup;
4344
}
4445

4546
if (opt_p || opt_l)
4647
die("%s", usage);
4748

4849
url1 = url_normalize(argv[1], NULL);
4950
url2 = url_normalize(argv[2], NULL);
50-
return (url1 && url2 && !strcmp(url1, url2)) ? 0 : 1;
51+
ret = (url1 && url2 && !strcmp(url1, url2)) ? 0 : 1;
52+
cleanup:
53+
free(url1);
54+
free(url2);
55+
return ret;
5156
}

0 commit comments

Comments
 (0)