]> git.ipfire.org Git - thirdparty/git.git/commit - midx.h
midx.c: write MIDX filenames to strbuf
authorTaylor Blau <me@ttaylorr.com>
Tue, 26 Oct 2021 21:01:21 +0000 (17:01 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 28 Oct 2021 22:32:14 +0000 (15:32 -0700)
commit60980aed786487e9113f0cb2907dfc75a77d363c
tree1fcd95354a08daa38ab957f5f81c3409e371a250
parentee4a1d63d7e9bdbea6bbeeb3f82ef33030de9ffb
midx.c: write MIDX filenames to strbuf

To ask for the name of a MIDX and its corresponding .rev file, callers
invoke get_midx_filename() and get_midx_rev_filename(), respectively.
These both invoke xstrfmt(), allocating a chunk of memory which must be
freed later on.

This makes callers in pack-bitmap.c somewhat awkward. Specifically,
midx_bitmap_filename(), which is implemented like:

    return xstrfmt("%s-%s.bitmap",
                   get_midx_filename(midx->object_dir),
                   hash_to_hex(get_midx_checksum(midx)));

this leaks the second argument to xstrfmt(), which itself was allocated
with xstrfmt(). This caller could assign both the result of
get_midx_filename() and the outer xstrfmt() to a temporary variable,
remembering to free() the former before returning. But that involves a
wasteful copy.

Instead, get_midx_filename() and get_midx_rev_filename() take a strbuf
as an output parameter. This way midx_bitmap_filename() can manipulate
and pass around a temporary buffer which it detaches back to its caller.

That allows us to implement the function without copying or open-coding
get_midx_filename() in a way that doesn't leak.

Update the other callers of get_midx_filename() and
get_midx_rev_filename() accordingly.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
midx.c
midx.h
pack-bitmap.c
pack-revindex.c