Skip to content

Commit 2eec0f5

Browse files
committed
Merge branch 'jk/unleak-mmap'
Plug a few leaks where mmap'ed memory regions are not unmapped. * jk/unleak-mmap: meson: turn on NO_MMAP when building with LSan Makefile: turn on NO_MMAP when building with LSan object-file: fix mmap() leak in odb_source_loose_read_object_stream() pack-revindex: avoid double-loading .rev files check_connected(): fix leak of pack-index mmap check_connected(): delay opening new_pack
2 parents c563b12 + a8a69bb commit 2eec0f5

5 files changed

Lines changed: 26 additions & 21 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,7 @@ BASIC_CFLAGS += -DSHA1DC_FORCE_ALIGNED_ACCESS
16051605
endif
16061606
ifneq ($(filter leak,$(SANITIZERS)),)
16071607
BASIC_CFLAGS += -O0
1608+
NO_MMAP = CatchMapLeaks
16081609
SANITIZE_LEAK = YesCompiledWithIt
16091610
endif
16101611
ifneq ($(filter address,$(SANITIZERS)),)

connected.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,6 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
4545
return err;
4646
}
4747

48-
if (transport && transport->smart_options &&
49-
transport->smart_options->self_contained_and_connected &&
50-
transport->pack_lockfiles.nr == 1 &&
51-
strip_suffix(transport->pack_lockfiles.items[0].string,
52-
".keep", &base_len)) {
53-
struct strbuf idx_file = STRBUF_INIT;
54-
strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
55-
base_len);
56-
strbuf_addstr(&idx_file, ".idx");
57-
new_pack = add_packed_git(the_repository, idx_file.buf,
58-
idx_file.len, 1);
59-
strbuf_release(&idx_file);
60-
}
61-
6248
if (repo_has_promisor_remote(the_repository)) {
6349
/*
6450
* For partial clones, we don't want to have to do a regular
@@ -90,7 +76,6 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
9076
promisor_pack_found:
9177
;
9278
} while ((oid = fn(cb_data)) != NULL);
93-
free(new_pack);
9479
return 0;
9580
}
9681

@@ -127,15 +112,27 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
127112
else
128113
rev_list.no_stderr = opt->quiet;
129114

130-
if (start_command(&rev_list)) {
131-
free(new_pack);
115+
if (start_command(&rev_list))
132116
return error(_("Could not run 'git rev-list'"));
133-
}
134117

135118
sigchain_push(SIGPIPE, SIG_IGN);
136119

137120
rev_list_in = xfdopen(rev_list.in, "w");
138121

122+
if (transport && transport->smart_options &&
123+
transport->smart_options->self_contained_and_connected &&
124+
transport->pack_lockfiles.nr == 1 &&
125+
strip_suffix(transport->pack_lockfiles.items[0].string,
126+
".keep", &base_len)) {
127+
struct strbuf idx_file = STRBUF_INIT;
128+
strbuf_add(&idx_file, transport->pack_lockfiles.items[0].string,
129+
base_len);
130+
strbuf_addstr(&idx_file, ".idx");
131+
new_pack = add_packed_git(the_repository, idx_file.buf,
132+
idx_file.len, 1);
133+
strbuf_release(&idx_file);
134+
}
135+
139136
do {
140137
/*
141138
* If index-pack already checked that:
@@ -162,6 +159,9 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
162159
err = error_errno(_("failed to close rev-list's stdin"));
163160

164161
sigchain_pop(SIGPIPE);
165-
free(new_pack);
162+
if (new_pack) {
163+
close_pack(new_pack);
164+
free(new_pack);
165+
}
166166
return finish_command(&rev_list) || err;
167167
}

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,7 @@ else
14381438
'getpagesize' : [],
14391439
}
14401440

1441-
if get_option('b_sanitize').contains('address')
1441+
if get_option('b_sanitize').contains('address') or get_option('b_sanitize').contains('leak')
14421442
libgit_c_args += '-DNO_MMAP'
14431443
libgit_sources += 'compat/mmap.c'
14441444
else

object-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ int odb_source_loose_read_object_stream(struct odb_read_stream **out,
22112211
return 0;
22122212
error:
22132213
git_inflate_end(&st->z);
2214-
munmap(st->mapped, st->mapsize);
2214+
munmap(mapped, mapsize);
22152215
free(st);
22162216
return -1;
22172217
}

pack-revindex.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ int load_pack_revindex_from_disk(struct packed_git *p)
277277
{
278278
char *revindex_name;
279279
int ret;
280+
281+
if (p->revindex_data)
282+
return 0;
283+
280284
if (open_pack_index(p))
281285
return -1;
282286

0 commit comments

Comments
 (0)