Skip to content

Commit 931847a

Browse files
pks-tttaylorr
authored andcommitted
list-objects-filter-options: work around reported leak on error
This one is a little bit more curious. In t6112, we have a test that exercises the `git rev-list --filter` option with invalid filters. We execute git-rev-list(1) via `test_must_fail`, which means that we check for leaks even though Git exits with an error code. This causes the following leak: Direct leak of 27 byte(s) in 1 object(s) allocated from: #0 0x5555555e6946 in realloc.part.0 lsan_interceptors.cpp.o #1 0x5555558fb4b6 in xrealloc wrapper.c:137:8 #2 0x5555558b6e06 in strbuf_grow strbuf.c:112:2 #3 0x5555558b7550 in strbuf_add strbuf.c:311:2 #4 0x5555557c1a88 in strbuf_addstr strbuf.h:310:2 #5 0x5555557c1d4c in parse_list_objects_filter list-objects-filter-options.c:261:3 #6 0x555555885ead in handle_revision_pseudo_opt revision.c:2899:3 #7 0x555555884e20 in setup_revisions revision.c:3014:11 #8 0x5555556c4b42 in cmd_rev_list builtin/rev-list.c:588:9 #9 0x5555555ec5e3 in run_builtin git.c:483:11 #10 0x5555555eb1e4 in handle_builtin git.c:749:13 #11 0x5555555ec001 in run_argv git.c:819:4 #12 0x5555555eaf94 in cmd_main git.c:954:19 #13 0x5555556fd569 in main common-main.c:64:11 #14 0x7ffff7ca714d in __libc_start_call_main (.../lib/libc.so.6+0x2a14d) #15 0x7ffff7ca7208 in __libc_start_main@GLIBC_2.2.5 (.../libc.so.6+0x2a208) #16 0x5555555ad064 in _start (git+0x59064) This leak is valid, as we call `die()` and do not clean up the memory at all. But what's curious is that this is the only leak reported, because we don't clean up any other allocated memory, either, and I have no idea why the leak sanitizer treats this buffer specially. In any case, we can work around the leak by shuffling things around a bit. Instead of calling `gently_parse_list_objects_filter()` and dying after we have modified the filter spec, we simply do so beforehand. Like this we don't allocate the buffer in the error case, which makes the reported leak go away. It's not pretty, but it manages to make t6112 leak free. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Taylor Blau <me@ttaylorr.com>
1 parent 3ec42f3 commit 931847a

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

list-objects-filter-options.c

+7-10
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,14 @@ void parse_list_objects_filter(
252252
const char *arg)
253253
{
254254
struct strbuf errbuf = STRBUF_INIT;
255-
int parse_error;
256255

257256
if (!filter_options->filter_spec.buf)
258257
BUG("filter_options not properly initialized");
259258

260259
if (!filter_options->choice) {
260+
if (gently_parse_list_objects_filter(filter_options, arg, &errbuf))
261+
die("%s", errbuf.buf);
261262
strbuf_addstr(&filter_options->filter_spec, arg);
262-
263-
parse_error = gently_parse_list_objects_filter(
264-
filter_options, arg, &errbuf);
265263
} else {
266264
struct list_objects_filter_options *sub;
267265

@@ -271,18 +269,17 @@ void parse_list_objects_filter(
271269
*/
272270
transform_to_combine_type(filter_options);
273271

274-
strbuf_addch(&filter_options->filter_spec, '+');
275-
filter_spec_append_urlencode(filter_options, arg);
276272
ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
277273
filter_options->sub_alloc);
278274
sub = &filter_options->sub[filter_options->sub_nr - 1];
279275

280276
list_objects_filter_init(sub);
281-
parse_error = gently_parse_list_objects_filter(sub, arg,
282-
&errbuf);
277+
if (gently_parse_list_objects_filter(sub, arg, &errbuf))
278+
die("%s", errbuf.buf);
279+
280+
strbuf_addch(&filter_options->filter_spec, '+');
281+
filter_spec_append_urlencode(filter_options, arg);
283282
}
284-
if (parse_error)
285-
die("%s", errbuf.buf);
286283
}
287284

288285
int opt_parse_list_objects_filter(const struct option *opt,

t/t6112-rev-list-filters-objects.sh

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test_description='git rev-list using object filtering'
55
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
66
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
# Test the blob:none filter.

0 commit comments

Comments
 (0)